12-24java object-oriented synchronization and deadlock

Source: Internet
Author: User
Tags ticket

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

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.