码迷,mamicode.com
首页 > 编程语言 > 详细

多线程 同步问题

时间:2018-07-22 15:10:39      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:dex   class   sys   public   ring   str   color   main   one   

public class TicketDemo 
{
    public static void main(String[] args) 
    {
        Ticket t=new Ticket();
        Thread t1=new Thread(t);
        t1.setName("One--");
        t1.start();
        Thread t2=new Thread(t);
        t2.setName("Two--");
        t2.start();
        Thread t3=new Thread(t);
        t3.setName("Three--");
        t3.start();
    }
}
class Ticket implements Runnable
{
    private int tick=100;
    Object obj=new Object();
    public void run()
    {
        while(true)
        {
            synchronized(obj)
            {
                if(tick>0)
                {
                    try
                    {
                        Thread.sleep(10);
                    }
                    catch (InterruptedException e)
                    {
                        System.out.println("中断异常");
                    }
                    System.out.println(Thread.currentThread().getName()+tick--);
                }
            }
        }
    }
}

 

多线程 同步问题

标签:dex   class   sys   public   ring   str   color   main   one   

原文地址:https://www.cnblogs.com/zhujialei123/p/9349871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!