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