Multi-threaded cyclic printing A B--face question

Source: Internet
Author: User
Tags sleep
Multi-threaded loop printing A B

Idea: Use the wait and notify using lock to utilize condition using synchronized (code such as lock ellipsis) to utilize the CAS variable Atomicinteger (code with lock omitted) wait and notify

The code is as follows

public class Lockdemo implements runnable{private lock lock;//Lock private String letters;//print Letter private int P Rintstatus;

    Control printing time private static Atomicinteger amount = new Atomicinteger (1);
        Public Lockdemo (lock lock, String letter, int printstatus) {this.lock = lock;
        This.letter = letter;
    This.printstatus = Printstatus;
            public void Run () {while (true) {Lock.lock ();
            try {thread.sleep (200);
            } catch (Interruptedexception e) {e.printstacktrace ();
                } if (Amount.get ()%2 = = Printstatus) {amount.incrementandget ();
            System.out.println ("Current thread:" + thread.currentthread (). GetName () + "letters:" + letter);
        }//other cases even if acquired to the lock empty run Lock.unlock ();
        }} public static void Main (string[] args) {lock lock = new Reentrantlock ();
 Lockdemo lockDemo1 = new Lockdemo (lock, "A", 1);       Lockdemo LockDemo2 = new Lockdemo (lock, "B", 0);
        New Thread (LOCKDEMO1, "Thread A"). Start ();
    New Thread (LockDemo2, "thread B"). Start (); }
}
Lock

The code is as follows

public class Lockdemo implements runnable{private lock lock;//Lock private String letters;//print Letter private int P Rintstatus;

    Control printing time private static Atomicinteger amount = new Atomicinteger (1);
        Public Lockdemo (lock lock, String letter, int printstatus) {this.lock = lock;
        This.letter = letter;
    This.printstatus = Printstatus;
            public void Run () {while (true) {Lock.lock ();
            try {thread.sleep (200);
            } catch (Interruptedexception e) {e.printstacktrace ();
                } if (Amount.get ()%2 = = Printstatus) {amount.incrementandget ();
            System.out.println ("Current thread:" + thread.currentthread (). GetName () + "letters:" + letter);
        }//other cases even if acquired to the lock empty run Lock.unlock ();
        }} public static void Main (string[] args) {lock lock = new Reentrantlock ();
 Lockdemo lockDemo1 = new Lockdemo (lock, "A", 1);       Lockdemo LockDemo2 = new Lockdemo (lock, "B", 0);
        New Thread (LOCKDEMO1, "Thread A"). Start ();
    New Thread (LockDemo2, "thread B"). Start (); }
}
Condition

The code is as follows

public class Conditiontask implements Runnable {private lock lock;
    Private Condition Condition;
    private String Letter;
        Public Conditiontask (lock lock, Condition Condition, String letter) {This.lock = lock;
        This.condition = condition;
    This.letter = letter;
        } public void Run () {lock.lock (); while (true) {try {condition.signalall ();//and Object.notifyall similar to SYSTEM.OUT.PR
                Intln ("Current thread:" + thread.currentthread (). GetName () + "letters:" + letter);
            Condition.await ();
            } catch (Interruptedexception e) {e.printstacktrace ();
        }}} public static void Main (string[] args) {lock lock = new Reentrantlock ();
        Condition Condition = Lock.newcondition ();
        New Thread (New Conditiontask (lock, condition, "a"), "a"). Start ();
    New Thread (New Conditiontask (lock, condition, "B"), "B"). Start (); }
}

If you print a B C, you need more than one state variable for control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.