Java multithreaded Programming (iii) Priority, synchronization, and deadlock of threads

Source: Internet
Author: User
Tags ticket

Priority of the thread:

The priority of the thread is divided into three types, namely:

1-min_priority

10-max_priority

5-norm_priority

If nothing is set the default value is 5

The priority of a thread can affect the order in which threads are executed, and of course this refers to a possible impact that does not necessarily affect it. In the default state (for example, the main thread) its default value is 5

Specific Code Demo:

Package Com.yeqc.thread;class ThRun implements runnable{@Overridepublic void Run () {for (int i=0; i<5; i++) {try {thread . Sleep (1000); System.out.println (Thread.CurrentThread (). GetName () + ":" +i); catch (Interruptedexception e) {e.printstacktrace ();}}}} public class ThreadDemo03 {public static void main (string[] args) {thread T1 = new Thread (new ThRun (), "A"); Thread t2 = new Thread (new ThRun (), "B"); thread t3 = new Thread (new ThRun (), "C"), t1.setpriority (thread.min_priority); t2.setpriority (thread.norm_priority); T3.setpriority (thread.max_priority); T1.start (); T2.start (); T3.start ();}}

Operation Result:

C:0a:0b:0c:1b:1a:1c:2a:2b:2c:3a:3b:3c:4b:4a:4


Sync and Deadlock:

1. Synchronizing code blocks

When you add the "synchronized" keyword to a code block, this code block is called a synchronous code block

2. Synchronous code block format:

Sychronized (Synchronization object) {block of code that needs to be synchronized; }

3. Synchronization method

In addition to the code block can be synchronized, the method can be synchronized

4. Method Synchronization Format:

sychronized void Method Name () {}

Specific Code Demo:

1. Synchronous code block mode:

Package Com.yeqc.thread;class Mythreaddemo implements runnable{private int ticket = 5, @Overridepublic void Run () {for (int i = 0; I < 10; i++) {synchronized (this) {if (ticket>0) {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ( );} System.out.println ("Ticket:" +ticket--);}}}} public class ThreadDemo04 {public static void main (string[] args) {Mythreaddemo m = new Mythreaddemo (); thread T1 = new Thread (m); Thread t2 = new Thread (m); thread t3 = new Thread (m); T1.start (); T2.start (); T3.start ();}}

Operation Result:

Ticket: 5 Tickets: 4 Tickets: 3 Tickets: 2 Tickets: 1

2. Synchronization method

Package Com.yeqc.thread;class Mythreaddemo implements runnable{private int ticket = 5, @Overridepublic void Run () {for (int i = 0; I < 10; i++) {tell ();}} Public synchronized void Tell () {if (ticket>0) {try {Thread.Sleep ($)} catch (Interruptedexception e) { E.printstacktrace ();} System.out.println ("Ticket:" +ticket--);}} public class ThreadDemo04 {public static void main (string[] args) {Mythreaddemo m = new Mythreaddemo (); thread T1 = new Thread (m); Thread t2 = new Thread (m); thread t3 = new Thread (m); T1.start (); T2.start (); T3.start ();}}

Operation Result:

Ticket: 5 Tickets: 4 Tickets: 3 Tickets: 2 Tickets: 1

/**

* Deadlock: Student Job search (hi-Tech)

* Business requires you to have work experience (experience)

*/


Java multithreaded Programming (iii) Priority, synchronization, and deadlock of threads

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.