Multi-thread-synchronous code block and multi-thread Synchronous Code

Source: Internet
Author: User

Multi-thread-synchronous code block and multi-thread Synchronous Code

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng

/**
* Created by Administrator on 2014/10/24.
* Synchronized locks the flag and changes the flag. When the flag reaches the same time point, only one thread is performing valid operations.
*/

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng


Public class TheadDemo5 {
Public static void main (String [] args)
{
ThreadTest t = new ThreadTest ();
Thread t1 = new Thread (t); t1.start ();
Thread t2 = new Thread (t); t2.start ();
Thread t3 = new Thread (t); t3.start ();
}
}

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng


Class ThreadTest implements Runnable {
Private int tickets = 100;
String str = new String ("");

Public void run (){
While (true ){
Synchronized (str ){
If (tickets> 0 ){
Try {
Thread. sleep (10 );
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
System. out. println (Thread. currentThread (). getName () + "is saling ticket" + tickets --);
}
}
}
}
}

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng

/**
* The result is
* Thread-0is saling ticket100
Thread-0is saling ticket99
Thread-0is saling ticket98.
Thread-0is saling ticket97
Thread-0is saling ticket96
Thread-0is saling ticket95
Thread-2is saling ticket94
Thread-2is saling ticket93.
Thread-2is saling ticket92
Thread-2is saling ticket91
Thread-2is saling ticket90
Thread-2is saling ticket89
Thread-2is saling ticket88
Thread-2is saling ticket87
Thread-2is saling ticket86.
Thread-2is saling ticket85
Thread-2is saling ticket84
Thread-2is saling ticket83
Thread-2is saling ticket82
Thread-2is saling ticket81
Thread-2is saling ticket80
Thread-2is saling ticket79
Thread-2is saling ticket78
Thread-2is saling ticket77.
Thread-2is saling ticket76
Thread-2is saling ticket75
Thread-2is saling ticket74.
Thread-2is saling ticket73
Thread-2is saling ticket72
Thread-2is saling ticket71.
Thread-2is saling ticket70
Thread-2is saling ticket69
Thread-2is saling ticket68
Thread-2is saling ticket67
Thread-2is saling ticket66
Thread-2is saling ticket65
Thread-2is saling ticket64
Thread-2is saling ticket63
Thread-2is saling ticket62.
Thread-2is saling ticket61
Thread-2is saling ticket60
Thread-2is saling ticket59
Thread-2is saling ticket58
Thread-2is saling ticket57.
Thread-2is saling ticket56.
Thread-2is saling ticket55
Thread-2is saling ticket54
Thread-2is saling ticket53
Thread-2is saling ticket52
Thread-2is saling ticket51.
Thread-2is saling ticket50
Thread-2is saling ticket49
Thread-2is saling ticket48
Thread-2is saling ticket47.
Thread-2is saling ticket46.
Thread-2is saling ticket45
Thread-2is saling ticket44
Thread-2is saling ticket43
Thread-2is saling ticket42
Thread-2is saling ticket41.
Thread-2is saling ticket40
Thread-2is saling ticket39
Thread-2is saling ticket38
Thread-2is saling ticket37
Thread-2is saling ticket36
Thread-2is saling ticket35
Thread-2is saling ticket34
Thread-2is saling ticket33.
Thread-2is saling ticket32.
Thread-2is saling ticket31
Thread-2is saling ticket30
Thread-2is saling ticket29.
Thread-2is saling ticket28
Thread-2is saling ticket27
Thread-2is saling ticket26
Thread-2is saling ticket25
Thread-2is saling ticket24
Thread-2is saling ticket23.
Thread-2is saling ticket22.
Thread-2is saling ticket21
Thread-2is saling ticket20
Thread-2is saling ticket19
Thread-2is saling ticket18.
Thread-2is saling ticket17.
Thread-2is saling ticket16.
Thread-2is saling ticket15
Thread-2is saling ticket14
Thread-2is saling ticket13
Thread-2is saling ticket12
Thread-2is saling ticket11
Thread-2is saling ticket10
Thread-2is saling ticket9
Thread-2is saling ticket8.
Thread-2is saling ticket7
Thread-2is saling ticket6
Thread-2is saling ticket5
Thread-2is saling ticket4
Thread-2is saling ticket3
Thread-2is saling ticket2
Thread-2is saling ticket1.

*/

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng
In java multithreading, when are synchronous code blocks used?

One is the lock method and the other is the Lock Object. What is your situation ??
If it is a multi-threaded operation on the same object, then lock the object, for example, the two methods of saving and taking the money... the operation is on the same account.
In this case, you need to lock the account object. For example, you can only buy one ticket at a time .. lock this method .. let only one thread enter this method... I do not know whether it is clear enough/

JAVA multi-thread synchronization

It is very simple. When it is not used, all threads are running this run, because you have no lock, that is to say, there seems to be only one runnable object, one run code block, however, four threads are in use, which is equivalent to 100 books in a room. Individuals can access the room at the same time. Naturally, all four threads are involved, when you lock up, it is equivalent to locking the door when a person enters, and other people cannot. Only when that person comes out, other people can go in. However, there is an endless loop in it. I know that the book has been put into a sack and guessed it. Other people have gone in, so the loop exits directly. In this way, it seems that only one thread is executing.

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.