Multi-threaded face test (thunderbolt)

Source: Internet
Author: User

Write a program that opens3A thread, which3Threads ofIdrespectively forA、B、Cid print 10 all times, the output must be abcabcabc.  My idea is this: each thread has a public lock, who first get the lock, who will judge is not my output ID, if not my output, I will release the lock, and continue to apply waiting for the lock (wait), if it is my output, I will output, and release the lock and wake up those waiting to request the lock thread (notify), how to control the A->b->c->a such a loop output, because the wake-up thread is indeterminate, if a thread wakes up the C thread, then the C thread must determine whether the output, if not When it's your turn, you have to release the lock and wake up the other person until the thread that wakes up is B.  Here's my implementation:Public class Abcoutputthread implements runnable{private static String now = "A";private final static String lock = "lock";private String Name,next;    Public abcoutputthread (String name,string next) {this.name = name;this.next = next;    } @OverridePublic void Run () {int i=10;While (i>0) {//System.out.print (this.name+ "Begin to get Lock");synchronized (lock) {//System.out.print (this.name+ "Get It");if (This.name.equals (now)) {//System.out.print (this.name+ "My Turn");System.out.print (this.name);Now = Next;I.;//System.out.print (this.name+ "Notify other Wait Thread");try {lock.notifyall ();} catch (Illegalmonitorstateexception e) {e.printstacktrace ();}}else{//System.out.print (this.name+ "Not my turn and wait for the other notify Me");try {lock.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}                }             }         }     } Public static void Main (string[] args) {thread T1 = new Thread (New Abcoutputthread ("A", "B"));thread t2 = new Thread (New Abcoutputthread ("B", "C"));thread t3 = new Thread (New Abcoutputthread ("C", "A"));T1.start ();T2.start ();T3.start ();    } } At first I wrote Lock.notify (), not Lock.notifyall (), so that when I ran the output ABC only, but not the loop 10 times the effect, but the debug can loop 10 times. This is why, later in the program code to add theSystem.out.print Debug Statement, found that notify will only wake up a thread, consider such a scenario:1, A to obtain the lock, successfully output a after releasing the lock, Wake up a thread2, happen to wake up is their own , found that should not turn their own output, the release lock wait, but did not wake any thread3, while BC is waiting for a wake-up, a does not wake anyone, it calls wait, so three threads all "asleep" Notify is the meaning of waking up a thread, waking only one, waking up to someone else or yourself, is not certain, and if Notifyall will wake up all the threads, so that all the threads have the opportunity to fight with the lock, personally feel that this is a waste of resources suspected, Next I'll see if there's a way to wake up a particular object and wake up exactly.  Baidu checked, did not find the exact way to wake!!!!!!!!!!!!!!!

Multi-threaded face test (thunderbolt)

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.