Multithreading-Synchronizing code blocks

Source: Internet
Author: User

Copyright?liupengcheng
Http://www.cnblogs.com/liupengcheng

/**
* Created by Administrator on 2014/10/24.
* Synchronized lock flag, change the flag bit, has reached the same time only one thread is performing a valid operation.
*/

Copyright?liupengcheng
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 ();
}
}

Copyright?liupengcheng
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--);
}
}
}
}
}

Copyright?liupengcheng
Http://www.cnblogs.com/liupengcheng

/**
* Results are
* 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

*/

Copyright?liupengcheng
Http://www.cnblogs.com/liupengcheng

Multithreading-Synchronizing code blocks

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.