多线程练习(简单模拟火车站多窗口同时售票)

  • 模拟火车站售票窗口同时售票
public class xianchenglianxi {
    public static void main(String arg[]){
        long begin = System.currentTimeMillis();
        MyThread myth_1 = new MyThread("1");        //创建线程对象
        MyThread myth_2 = new MyThread("2");
        MyThread myth_3 = new MyThread("3");
        MyThread myth_4 = new MyThread("4");
        MyThread myth_5 = new MyThread("5");
        myth_1.setPriority(5);        //设置线程优先级 1最低 10最高
        myth_2.setPriority(6);
        myth_3.setPriority(10);
        myth_4.setPriority(1);
        myth_5.setPriority(8);
        myth_1.start();                //启动线程
        myth_2.start();
        myth_3.start();
        myth_4.start();
        myth_5.start();
    }
}
class MyThread extends Thread {        //继承Thread类,为了调用其run方法
    String name;
    int ticket = 5;            //票总数
    public MyThread(String name){
        this.name = name;
    }
    public void run(){                //方法重写
        for(int i = ticket;i >= 0;i--){
            if(i > 0){
                System.out.println("窗口:"+name+"售票成功"+"\t"+"余票"+i);
            }
            else{
                System.out.println("窗口:"+name+"售票成功\t余票 "+i+"\t窗口关闭");
            }
        }

    }
}
时间: 2024-08-25 18:51:08

多线程练习(简单模拟火车站多窗口同时售票)的相关文章

创建两个线程模拟火车站两个窗口售票程序

题目:创建两个线程模拟火车站两个窗口售票程序,窗口售票时间为1秒,两个窗口不能同时售票 #include<Windows.h> #include<iostream> using namespace std; //这是2个线程模拟买火车票的小程序 DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data int inde

Java 多线程IO简单实用Demo

多线程主要作用是充分利用Cpu,而不在于它的乱序性.本Demo不讲它竞争什么的.之前看过乱序打印ABC的例子什么的,那些有意义吗? 本Demo 是多线程打印文件夹下的文件,主要实现是用数组存放文件,一个游标遍历. 我们需要考虑在什么时候加互斥访问,本例用synchronized . 先考虑单线程的流程:客户端启动-->读取文件下的文件放到数组(IO)--> 取游标打印 ,游标加1 -- > 改文件写文件(IO) -- 重复上两步至越界 -- 结束 多线程时显然需要在"取游标打印

7-18 银行业务队列简单模拟

7-18 银行业务队列简单模拟(25 分) 设某银行有A.B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 -- 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客.给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列.假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出. 输入格式: 输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号.编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口.数字

HDU-1034-Candy Sharing Game(C++ &amp;&amp; 简单模拟)

Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3703    Accepted Submission(s): 2311 Problem Description A number of students sit in a circle facing their teacher in the cent

gdb调试多线程的简单命令

由于平时的编程当中经常会用到多线程,我想接触过多线程编程的人应该都会清楚多线程如果出了错,调起bug来是有多么麻烦,应为你某个地方出错了,有可能并不是这里的问题而是线程执行到这就切换到其他线程,而出错的是其他线程,我以前使用的办法是给某个线程sleep然后让内核自己调度去执行其他线程.很明显这种方法当有很多线程是并不是很使用,所以我就翻书学了几条调试多线程的简单gdb命令 1.测试所用的代码 1void *thread1(void *arg) 2{ 3 printf("New thread1\n

Jquery源码分析与简单模拟实现

前言 最近学习了一下jQuery源码,顺便总结一下,版本:v2.0.3 主要是通过简单模拟实现jQuery的封装/调用.选择器.类级别扩展等.加深对js/Jquery的理解. 正文 先来说问题: 1.jQuery为什么能使用$的方式调用,$是什么.$()又是什么.链式调用如何实现的 2.jQuery的类级别的扩展内部是怎样实现的,方法级别的扩展有是怎样实现的,$.fn又是什么 3.jQuery选择器是如何执行的,又是如何将结果包装并返回的 带着这些问题,我们进行jquery的模拟实现,文章下方有

Linux 内核 链表 的简单模拟(2)

接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) for (pos = (head)->next; pos != (head);

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in

JavaWeb学习总结(四十九)——简单模拟Sping MVC

在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: 1 /** 2 * 使用Controller注解标注LoginUI类 3 */ 4 @Controller 5 public class LoginUI { 6 7 //使用RequestMapping注解指明forward1方法的访问路径 8 @RequestMapping("LoginUI/Lo