`
flyinsky
  • 浏览: 2139 次
  • 性别: Icon_minigender_1
  • 来自: 福建
文章分类
社区版块
存档分类
最新评论

java thread

    博客分类:
  • java
 
阅读更多
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


分享到:
评论

相关推荐

    JavaThread

    JavaThread

    Java Thread用法 经典

    ### Java Thread用法详解 #### 一、Java线程基础概念与重要性 在Java编程语言中,线程是程序执行的基本单位之一,它能够帮助我们实现多任务处理,提高程序运行效率。Java中的线程主要通过`java.lang.Thread`类来...

    Java Thread Programming

    本资料“Java Thread Programming”由Paul Hyde提供,包含了关于Java线程编程的理论知识和实践代码,旨在帮助开发者深入理解和熟练掌握Java线程。 首先,我们来了解一下Java中线程的基本概念。在Java中,可以通过两...

    JAVA thread

    在Java编程语言中,线程(Thread)是执行单元,它允许程序同时执行多个任务。在"JAVA thread"这个主题中,我们主要关注的是如何在Java中创建和管理线程,以及如何通过线程实现并发执行,就像"龟兔赛跑"这个小游戏所...

    java thread dump 分析

    Java Thread Dump 分析 Java Thread Dump 分析是 Java 应用程序性能优化的重要工具之一。Thread Dump 是 JVM 的一个快照,记录了当前所有线程的状态,包括线程的 ID、名称、状态、锁信息等。通过分析 Thread Dump,...

    Java Thread Programming (Sams) java线程编程(含code)

    本资源“Java Thread Programming (Sams)”提供了详细的线程编程知识,结合了理论与实际代码,旨在帮助开发者深入理解并熟练掌握Java线程。 1. **线程概念** - 线程是操作系统调度的基本单位,一个进程可以包含多...

Global site tag (gtag.js) - Google Analytics