Java Multithreading: Combine multithreading alternating print 10 times ABC deepen understanding of wait/notify __ Threads

Source: Internet
Author: User
Tags prev

Connect the article: http://blog.csdn.net/wabiaozia/article/details/79429585

There's a friend. Click on the three threads in the open link "a thread prints 10 times a,b thread prints 10 times b,c thread prints 10 times C, requires the thread to run simultaneously, alternating print 10 times ABC. "The code for this problem is a bit less understood, so let me see." The reason for not understanding is also very simple, is not deep understanding of wait and notify, encounter complex point of the scene will be confused. For concurrency related things, very recommended books concurrent programming combat (Dong Yunlan translation). This blog is purely for intuitive understanding and write, so when the words are not accurate, if you want to see the precise definition of some concepts, suggest reading, do not read this article.

Text:

Some concepts:

1 wait releases the lock and causes the current thread to hibernate.

2 One thing to note is that after the Notify () call, instead of releasing the object lock immediately, the lock is automatically released at the end of the corresponding synchronized () {} statement block.

Combined with code understanding:

Q 1: "Wait will release the lock and make the current thread hibernate" understanding

The first time a thread executes first, new Thread (PA). Start (). In code 1 and 2 have prev (c lock) and self (a lock) respectively. For starters, in the a thread process, what are the points to be understood in code 4?

1th, wait will release the lock, release whose lock. Because it is prev.wait (), so the release is Prev lock, namely C lock. 2nd, causes the thread to hibernate, which thread sleeps. Is that because it is prev.wait (), so the dormant is prev (c lock) corresponding C thread. No, the hibernation here is the currently running thread hibernation, that is, a thread hibernate, not C thread hibernation. 3rd, how to wake up a thread, using A.notify or c.notify. Does it mean that the current thread is dormant and wakes (a.notify) with the current thread? No, the front is c.wait (that is, the prev.wait in the code, the execution time parameter prev refers to C) makes thread a hibernate. So this still needs to be c.notify wake thread A.

Ask 2:notify () after the call, will immediately release the object lock

At code 3 below, the Notify () call does not immediately release the object lock, but instead ends the corresponding synchronized () {} statement block and automatically releases the lock.

Question 3: Summary below the execution flow of a thread

Code 1 and 2, respectively, are C and a locks, which is well understood. Release a lock at code 3. Code 4, Release C lock, while hibernation a thread, the next need c.notify () to wake up a thread.

Question 4: Is there any other solution? Reprint indicates link: http://blog.csdn.net/wabiaozia/article/details/79534560

Have lock lock, reentrantlock combine condition, signal quantity semaphore. I blog all posts Link: Http://blog.csdn.net/wabiaozia

/** * @author Dreamsea * @time 2015.3.9 * * Package com.multithread.wait;   
    public class MyThreadPrinter2 implements Runnable {private String name;   
    Private Object prev;   
  
    Private Object self;   
        Private MyThreadPrinter2 (String name, Object prev, object self) {this.name = name;   
        This.prev = prev;   
    This.self = self;   
        @Override public void Run () {int count = 10; while (Count > 0) {synchronized (prev) {//1 Code 1 synchronized (self) {//2 code 2   
                    Department System.out.print (name);  
                    
                    count--;   Self.notify ();   3 Code 3 at the try {prev.wait ();   
                4 Code 4 at the catch (Interruptedexception e) {e.printstacktrace ();   
   }   
            }   
  
        } public static void Main (string[] args) throws Exception {Object A = new object ();   
        Object B = new Object ();   
        Object c = new Object ();   
        MyThreadPrinter2 pa = new MyThreadPrinter2 ("A", C, a);   
        MyThreadPrinter2 PB = new MyThreadPrinter2 ("B", A, b);   
           
           
        MyThreadPrinter2 pc = new MyThreadPrinter2 ("C", B, c);
        New Thread (PA). Start ();  Thread.Sleep (100);
        Make sure that new Thread (PB) is executed in order a, B, and C. Start ();  
        Thread.Sleep (100);   
        New Thread (PC). Start ();  
        Thread.Sleep (100);  

 }   
}




Related Article

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.