Java object-oriented synchronization and deadlock

Source: Internet
Author: User

Java object-oriented synchronization and deadlock
Case 1

Design a thread operation class, which requires that three thread objects can be generated and the sleep time of three threads can be set.

Analysis:

1. Use the Thread class to implement

Class MyThread extends Thread {// encapsulation attribute private String name; // defines the Thread name private int time; // defines the sleep time // constructor public MyThread (String name, int time) {super (name); this. time = time;} // overwrite the run method public void run () {System. out. println (Thread. currentThread (). getName () + start of sleep); 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 + duration: + this. time + MS) ;}} public class TestThread16 {public static void main (String [] args) {MyThread mt1 = new MyThread (thread A, 1000); mt1.start (); myThread mt2 = new MyThread (thread B, 2000); mt2.start (); MyThread mt3 = new MyThread (thread C, 3000); mt3.start ();}}

 

1. Use the Runnable interface

Class MyThread implements Runnable {// encapsulation attribute private String name; private int time; // constructor public MyThread (String name, int time) {this. name = name; this. time = time;} public String getName () {return this. name;} // override the run method public void run () {System. out. println (this. name + start of sleep); try {Thread. currentThread (). sleep (this. time);} catch (InterruptedException e) {System. out. println (this. name + sleep interrupt);} System. out. println (this. name + sleep + duration: + this. time + MS) ;}} public class TestThread17 {public static void main (String [] args) {MyThread mt1 = new MyThread (Thread A, 1000); new Thread (mt1 ). start (); MyThread mt2 = new MyThread (Thread B, 2000); new Thread (mt2 ). start (); MyThread mt3 = new MyThread (Thread C, 3000); new Thread (mt3 ). start ();}}

Synchronization

The problem arises: To understand each ticket point as a thread, each thread needs to share resources. If latency is introduced, the system may have a negative number.

Class Sale implements Runnable {private int ticket = 5; public void run () {for (int I = 0; I <50; ++ I) {if (ticket> 0) {System. out. println (tickets are successfully purchased, with the remaining ticcket = + ticket --) ;}}} public class TestChronized1 {public static void main (String [] args) {Sale s = new Sale (); Thread th1 = new Thread (s); Thread th1 = new Thread (s); Thread th3 = new Thread (s); th1.start (); th2.start (); th3.start ();}}

Latency 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 (tickets are successfully purchased, with the remaining ticcket = + ticket --) ;}}} public class TestChronized1 {public static void main (String [] args) {Sale s = new Sale (); Thread th1 = new Thread (s); Thread th1 = new Thread (s); Thread th3 = new Thread (s); th1.start (); th2.start (); th3.start ();}}

Result


 

To solve the Data Sharing Problem, use the synchronous code block or the same method.

Synchronous Code Block
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 (tickets are successfully purchased, with the remaining ticcket = + ticket --) ;}}} public class TestChronized2 {public static void main (String [] args) {Sale s = new Sale (); Thread th1 = new Thread (s); Thread th1 = new Thread (s); Thread th3 = new Thread (s); th1.start (); th2.start (); th3.start ();}}

Synchronous Code block:

Synchronized (synchronization object ){}

The synchronization object is generally replaced by this.

 

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 (tickets are successfully purchased, with the remaining ticket count ticcket = + ticket --) ;}} public class TestChronized3 {public static void main (String [] args) {Sale s = new Sale (); Thread th1 = new Thread (s); Thread th1 = new Thread (s); Thread th3 = new Thread (s); th1.start (); th2.start (); th3.start ();}}

Identical results

Synchronized Method Return Value Method (){}

 

 

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.