Use of Java notify

Source: Internet
Author: User

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

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.