Thread Note One: Java Threads Classic topic: Rotate Print

Source: Internet
Author: User

Title: Build four threads, thread a adds variable 100 plus 1,b minus 1,c plus 1,d minus 1, executes in a->b->c->d order, and the final number is 100. Using multithreading

Code:

Printletter.java
Import Java.lang.thread;class MyThread implements runnable{//assign three objects to a thread private object Pre;private object Self;private Num num;public MyThread (Object pre,object self,num num) {this.pre=pre;this.self=self;this.num=num;} public void Run () {i=0;i<10;i++ (int) {synchronized (pre) {synchronized (self) {synchronized (num) {self.done (num, Thread.CurrentThread (). GetName ()); self.notify ();//Wake up other threads to release the object lock}}//otherwise or release the previous object Lock Try{pre.wait () after the run completes;} catch (Exception e) {e.printstacktrace ();}}}} Numeric object class Num{public int num=100;public void Add () {this.num++;} public void Resuce () {this.num--;} public int Get () {return this.num;}} Class Object{boolean done;//means add or subtract public Object (Boolean do) {This.done=done;} public void-Done (Num num,string name) {if (This.done) {num.add (); System.out.println ("+name+" add----one,then num= "+num.get ());} Else{num.resuce (); System.out.println ("+name+" reduce One,then num= "+num.get ());}}} public class Printletter{public static void Main (String args[]) {//Define four Objects Object First=new object (True); object two=new object (False), Object Three=new object (True), Object Four=new object (FALSE); Num num=new num (); MyThread mythread1=new MyThread (four,first,num); MyThread mythread2=new MyThread (first,two,num); MyThread mythread3=new MyThread (two,three,num); MyThread mythread4=new MyThread (three,four,num); new Thread (Mythread1). Start (); Try{thread.sleep (100);} catch (Exception e) {}new Thread (mythread2). Start (); Try{thread.sleep (100);} catch (Exception e) {}new Thread (mythread3). Start (); Try{thread.sleep (100);} catch (Exception e) {}new Thread (MYTHREAD4). Start (); Try{thread.sleep (100);} catch (Exception e) {}}}
Results:


Process parsing:

/* Four lock a b c D thread 1  critical section to get lock D a thread 2 critical section to get lock  a B thread 3 critical section to get lock  b C thread 4  critical section to get lock C D to execute thread 1 because thread 1 acquired lock A, so thread 2 waits for thread 1 to release lock a , thread 2 gets lock B thread 3 waits for thread 2 to lock B, thread 4 waits for lock C and D. After thread 1 executes the critical section, call A.notify, release the lock, then thread 2 is awakened, execution begins, and the chain Reaction thread 3 thread 4 is executed sequentially. In addition, thread 1 calls d.wait to wait for other threads to wake up, and when thread 4 is executed, calling D.notify will wake up thread 1. This process repeats until all threads have finished executing. Thread 1 2 3 4 will get sequential execution 1 wake 2   Execution 2 wake 3   Execution 3 Wake 4   Execution 4 wake-up 1:1-------------->2-------->3------------>4^                                       | | ---------------------------------------|*/


Thread Note One: Java Threads Classic topic: Rotate Print

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.