Halfway decent to learn Java, and took a few minutes to read it briefly. In the morning that example slightly changed the next,
My understanding is that Java synchronized is just a thread synchronization flag, but does not clog threads, while wait blocks the caller thread, class Windows plug-in function, while notify is equivalent to unlocking (which is understood by the individual as a critical section), the example below is mainly The default running thread Test1 will execute first, I change it a little, let the first thread hang, wait for the second thread to finish, then execute the first thread
Code
Import Java.util.concurrent.countdownlatch;public class Countdown {private static Countdownlatch connectedsignal = new Countdownlatch (), Static class MyThread implements Runnable {public void run () {Thread current = Thread.CurrentThread (); System.out.println (Current.getname ()); synchronized (connectedsignal) {Thread thread = Thread.CurrentThread (); String name = Thread.getname (), if (Name.equals ("Test1")) {try {connectedsignal.wait ();} catch (Interruptedexception e) { TODO auto-generated catch Blocke.printstacktrace ();}} for (int i = 0; i <; ++i) {sale ();} if (Name.equals ("Test2")) {connectedsignal.notify ();}}} Public synchronized void Sale () {if (Connectedsignal.getcount () > 0) {Thread current = Thread.CurrentThread (); connecte Dsignal.countdown (); SYSTEM.OUT.PRINTLN ("Thread id" + current.getid () + "value =" + Connectedsignal.getcount ());}}} public static void Main (string[] args) throws Interruptedexception {MyThread my = new MyThread (); thread T1 = new Thread (My, "test1"); Thread t2 = nEW Thread (My, "test2"); T1.start (); T2.start (); connectedsignal.await (); Wait to end System.out.println (Connectedsignal.getcount ());}}
It took a few minutes to change it a little, and let two threads take turns.
Import Java.util.concurrent.countdownlatch;public class Countdown {private static Countdownlatch connectedsignal = new Countdownlatch (), Static class MyThread implements Runnable {public void run () {Thread current = Thread.CurrentThread (); System.out.println (Current.getname ()); synchronized (connectedsignal) {Thread thread = Thread.CurrentThread (); String name = Thread.getname (),//if (Name.equals ("Test1")) {//try {//connectedsignal.wait ();/} catch ( Interruptedexception e) {////TODO auto-generated catch Block//e.printstacktrace ();//}//}for (int i = 0; i <; ++i) { if (Name.equals ("Test1") && (i% 2 = = 0)) {try {sale (); connectedsignal.notify (); connectedsignal.wait (); Connectedsignal.notify ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Else{sale (); connectedsignal.notify (); try {connectedsignal.wait (); Connectedsignal.notify ();} catch ( Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} if (Name.equals ("tEst2 ")) {//connectedsignal.notify ();//}}}public synchronized void sale () {if (Connectedsignal.getcount () > 0) { Thread current = Thread.CurrentThread (); Connectedsignal.countdown (); SYSTEM.OUT.PRINTLN ("Thread id" + current.getid () + "value =" + Connectedsignal.getcount ());}}} public static void Main (string[] args) throws Interruptedexception {MyThread my = new MyThread (); thread T1 = new Thread (My, "test1"); Thread t2 = new Thread (My, "test2"); T1.start (); T2.start (); connectedsignal.await (); Wait to end System.out.println (Connectedsignal.getcount ());}}
The result is the same as I thought. The sense of the thread itself is very simple, Java estimation is for developers more convenient development, their own packaging and provide more features, but I am not accustomed to.
Purely my point of view, there are mistakes welcome to point out.
Use of Java notify