2017.05.23 thread _ synchronized location and parameter problems, multi-thread synchronized

Source: Internet
Author: User

2017.05.23 thread _ synchronized location and parameter problems, multi-thread synchronized

The following is a piece of thread applet code that simulates a small program with two ticketing ports:

Class yuan implements Runnable {

Public static int piao = 1;

Public void run (){

While (piao <= 20 ){
Synchronized (""){
System. out. println (Thread. currentThread (). getName () + "currently sold" + piao + "tickets ");

Piao ++;
}
}
}

}

Public class ceshi6 {

Public void asd (){
Thread a = new Thread (new yuan ());
Thread B = new Thread (new yuan ());
B. setPriority (10 );
A. setName ("a port ");
B. setName ("B port ");
A. start ();
B. start ();
}

Public static void main (String [] args ){
Ceshi6 q = new ceshi6 ();
Q. asd ();
}
}

 

The execution result is as follows:

Port a currently sells 1st tickets
Port a currently sells 2nd tickets
Port B currently sells 3rd tickets
Port B currently sells 4th tickets
Port B currently sells 5th tickets
Port B currently sells 6th tickets
Port B currently sells 7th tickets
Port B currently sells 8th tickets
Port B currently sells 9th tickets
Port B currently sells 10th tickets
Port B currently sells 11th tickets
Port B currently sells 12th tickets
Port B currently sells 13th tickets
Port B currently sells 14th tickets
Port B currently sells 15th tickets
Port B currently sells 16th tickets
Port B currently sells 17th tickets
Port a currently sells 18th tickets
Port a currently sells 19th tickets
Port a currently sells 20th tickets
Port B currently sells 21st tickets

Q: It's strange why the 20-to-the-first loop is executed 21?

Cause: the thread does not necessarily switch when it is running while.

It may be because the two threads run simultaneously because they are blocked before synchronize. One condition is that both threads have entered the while wait, but only one thread can enter the synchronize at the same time. Therefore, assume that a enters B. still waiting

Piao + 1 and now piao is 21, and B waiting at the synchronize door will not end, but will enter synchronize and output with 21, and then both threads will not end when the while large condition thread is not met.

 

Change the code to the following (only change the run method, and nothing else will change)

Public void run (){

While (true ){
Synchronized ("aa "){
If (piao <= 20 ){
System. out. println (Thread. currentThread (). getName () + "currently sold" + piao + "tickets ");
Piao ++;
}
Else break;
}
}
}

 

Summary:

1. Multiple Threads simultaneously grab one cpu

2. setPriority. The default priority is 5.

3. synchronize: other threads will be blocked before the code execution in synchronize is completed by locking in a thread.

4. Add static to piao. If the object of a class is divided into different threads without static

5. The new columns are different each time. The column {

Thread a = new Thread (new yuan ());
Thread B = new Thread (new yuan ());

The implementation interface class objects corresponding to the two a and B threads are different.

}

 

Other Summary of the day:

 1. The object declared by final can only be constructed once. If there is still a structure under the Statement, the system prompts a syntax error.

  

 

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.