Case1
Design a threading action class that requires three thread objects to be generated, and can set the sleep time of three threads
Analysis :
1. Using the Thread class to implement
Class MyThread extends thread{//encapsulation property private String name;//define the name of the thread private int time;//define sleep Time//construction method public MyThread ( String name, int time) {super (name); this.time = time;} Overwrite the Run method public void run () {System.out.println (Thread.CurrentThread (). GetName () + "hibernate start"); Try{thread.currentthread ( ). Sleep (this.time);} catch (Interruptedexception e) {System.out.println (Thread.CurrentThread (). GetName () + "sleep Interrupt");} System.out.println (Thread.CurrentThread (). GetName () + "sleep End" + "persistent:" + this.time + "MS");}} public class Testthread16{public static void Main (string[] args) {MyThread mt1 = new MyThread ("Thread A", +); Mt1.start (); MyThread mt2 = new MyThread ("Thread B", "C"); Mt2.start (); MyThread mt3 = new MyThread ("Thread C", "N"); Mt3.start ();}}
1. Using the Runnable interface to implement
Class MyThread implements runnable{//encapsulation property private String name;p rivate int time;//Construction method public MyThread (String name, int ti Me) {this.name = name; this.time = time;} Public String GetName () {return this.name;} Overwrite the Run method public void run () {System.out.println (THIS.name + "hibernate start"); Try{thread.currentthread (). Sleep (this.time);} catch (Interruptedexception e) {System.out.println (this.name + "sleep Interrupt");} System.out.println (THIS.name + "sleep End" + "persistent:" + this.time + "MS");}} public class Testthread17{public static void Main (string[] args) {MyThread mt1 = new MyThread ("Thread A", "N"), new Thread (MT1). Start (); MyThread mt2 = new MyThread ("Thread B", "N"), new Thread (MT2). Start (); MyThread mt3 = new MyThread ("Thread C", "N"), new Thread (MT3). Start ();}}
Synchronous
The problem arises: by understanding each ticket point as a thread, each thread needs to share resources. If the delay is introduced, there may be a negative problem with the system.
Class Sale implements runnable{private int ticket = 5;public void Run () {for (int i =0;i<50; ++i) {if (ticket>0) {Sys Tem.out.println ("The ticket is successful, the votes are left ticcket=" + ticket--);}}} public class TestChronized1 {public static void main (string[] args) {Sale s = new Sale (); Thread Th1 = new thread (s); Thread Th2 = new thread (s); Thread th3 = new thread (s); Th1.start (); Th2.start (); Th3.start ();}}
Delay is introduced below
Class Sale implements runnable{private int ticket = 5;public void Run () {for (int i =0;i<50; ++i) {if (ticket>0) {try {Thread.Sleep (500);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("The ticket is successful, the votes are left ticcket=" + ticket--);}}} public class TestChronized1 {public static void main (string[] args) {Sale s = new Sale (); Thread Th1 = new thread (s); Thread Th2 = new thread (s); Thread th3 = new thread (s); Th1.start (); Th2.start (); Th3.start ();}}
Results
To solve data sharing problems-use synchronous code blocks or the same method.
Synchronizing code blocks
Class Sale implements runnable{private int ticket = 5;public void Run () {for (int i =0;i<50; ++i) {synchronized (this) { if (ticket>0) {try{thread.sleep (500);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("The ticket is successful, the votes are left ticcket=" + ticket--);}}}} public class TestChronized2 {public static void main (string[] args) {Sale s = new Sale (); Thread Th1 = new thread (s); Thread Th2 = new thread (s); Thread th3 = new thread (s); Th1.start (); Th2.start (); Th3.start ();}}
Synchronizing code blocks:
Synchronized (Synchronization object) {}
Where synchronization objects generally use this instead
Synchronization method
Class Sale implements runnable{private int ticket = 5;public void Run () {for (int i =0;i<50; ++i) {Saleticket ();}} Public synchronized void Saleticket () {if (ticket>0) {try{thread.sleep (500);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("The ticket is successful, there are still votes ticcket=" + ticket--);}} public class TestChronized3 {public static void main (string[] args) {Sale s = new Sale (); Thread Th1 = new thread (s); Thread Th2 = new thread (s); Thread th3 = new thread (s); Th1.start (); Th2.start (); Th3.start ();}}
The results are exactly the same
Synchronized Method Return value Method () {}
Dead lock
Different threads are waiting for each other
Summarize:
Synchronization is required as long as data sharing
Too much synchronization can create deadlocks.
12-24java object-oriented synchronization and deadlock