java thread

java Thread

闲来没事,回顾了一下java线程。写了个demo模拟以下场景:

10个线程进行售票,票数售到50时都等待,由唤醒线程进行唤醒,继续售完所有的票。



涉及的类:

Ticket.java   //ticket实体类

SellTicketThread.java // 售票线程

NotifyThread.java // 唤醒线程

MainTest.java  //主测试类


package com.yjz.thread;

public class Ticket {
    private int count;
    
    private boolean isStop = false;

    public Ticket() {
        super();
    }

    public Ticket(int count) {
        super();
        this.count = count;
    }

    public int getCount() {
        return count;
    }


    public void setCount(int count) {
        this.count = count;
    }


    public boolean isStop() {
        return isStop;
    }


    public void setStop(boolean isStop) {
        this.isStop = isStop;
    }


}

 

 

package com.yjz.thread;

public class SellTicketThread implements Runnable {
    
    
    Ticket ticket;
    
    String name;
    
    Object object;
    
    public SellTicketThread() {
        super();
    }
    
    
    public SellTicketThread(String name,Ticket ticket,Object object) {
        super();
        this.name = name;
        this.ticket = ticket;
        this.object = object;
    }


    @Override
    public void run() {
        while(ticket.getCount() > 0){
            synchronized (object) {
            
                if(ticket.getCount() ==  50)
                {
                    ticket.setStop(true);
                    try {
                        object.wait();
                        System.out.println(Thread.currentThread().getName()+" is waiting");
                        
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                
                if(!ticket.isStop())
                {
                    if(ticket.getCount() > 0)
                    {
                        ticket.setCount(ticket.getCount() - 1);
                        System.out.println(Thread.currentThread().getName()+":"+ticket.getCount());
                        try {
                            Thread.currentThread().sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    
                }
            }
        }
        
    }
    

}

 

 

package com.yjz.thread;

public class NotifyThread extends Thread {
    
    private Object object;
    
    Ticket ticket;
    
    public NotifyThread() {
        super();
    }

    public NotifyThread(Ticket ticket,Object object) {
        super();
        this.ticket = ticket;
        this.object = object;
    }


    public void run()
    {
        while(true)
        {
            synchronized (object) {
                if(ticket.isStop()){
                    object.notifyAll();
                    ticket.setStop(false);
                    break;
                }
            }
        }
    }
}

 

package com.yjz.thread;

public class MainTest {

    public static void main(String[] args) {
        Object object = new Object();
        Ticket ticket = new Ticket(100);
        
        for(int i = 0 ; i< 10; i++)
        {
            SellTicketThread temp = new SellTicketThread(String.valueOf(i),ticket,object);
            Thread thread = new Thread(temp);
            thread.start();
        }
    
        NotifyThread notifyThread = new NotifyThread(ticket,object);
        notifyThread.start();
        
    }
}


运行结果:

Thread-0:99
Thread-0:98
Thread-0:97
Thread-0:96
Thread-9:95
Thread-9:94
Thread-9:93
Thread-7:92
Thread-5:91
Thread-3:90
Thread-3:89
Thread-3:88
Thread-3:87
Thread-3:86
Thread-1:85
Thread-1:84
Thread-1:83
Thread-1:82
Thread-1:81
Thread-1:80
Thread-1:79
Thread-1:78
Thread-8:77
Thread-8:76
Thread-8:75
Thread-6:74
Thread-6:73
Thread-4:72
Thread-4:71
Thread-4:70
Thread-4:69
Thread-4:68
Thread-2:67
Thread-2:66
Thread-2:65
Thread-2:64
Thread-4:63
Thread-4:62
Thread-4:61
Thread-4:60
Thread-4:59
Thread-6:58
Thread-6:57
Thread-6:56
Thread-6:55
Thread-8:54
Thread-8:53
Thread-8:52
Thread-8:51
Thread-1:50
Thread-0 is waiting
Thread-0:49
Thread-0:48
Thread-0:47
Thread-0:46
Thread-9 is waiting
Thread-9:45
Thread-7 is waiting
Thread-7:44
Thread-7:43
Thread-7:42
Thread-7:41
Thread-5 is waiting
Thread-5:40
Thread-5:39
Thread-5:38
Thread-5:37
Thread-3 is waiting
Thread-3:36
Thread-3:35
Thread-3:34
Thread-3:33
Thread-3:32
Thread-1 is waiting
Thread-1:31
Thread-1:30
Thread-1:29
Thread-1:28
Thread-1:27
Thread-1:26
Thread-1:25
Thread-8:24
Thread-8:23
Thread-8:22
Thread-8:21
Thread-6:20
Thread-6:19
Thread-4:18
Thread-4:17
Thread-4:16
Thread-2:15
Thread-4:14
Thread-4:13
Thread-4:12
Thread-4:11
Thread-4:10
Thread-6:9
Thread-6:8
Thread-8:7
Thread-8:6
Thread-8:5
Thread-8:4
Thread-8:3
Thread-8:2
Thread-8:1
Thread-8:0


你可能感兴趣的:(thread)