Java——多线程---18.11.22

 

多线程代码:Runnable方法

package com.hebust.java.third;
 
import java.util.Random;
 
public class SaleTicket implements Runnable {
 
public int total;
public int count;
 
public SaleTicket() {
total = 100;
count = 0;
}
 
public void run() {
while (total > 0) {
synchronized (this) {
if(total > 0) {
try {
//Thread.sleep(800);
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
total--;
 
System.out.println(Thread.currentThread().getName() + "\t当前票号:" + count);
}
}
}
 
}
 
public static void main(String[] args) {
SaleTicket st = new SaleTicket();
for(int i=1; i<=5; i++) {
new Thread(st, "售票点" + i).start();
}
}
}

 

总结:这一节里了解了多线程的知识,用多线程解决火车卖票等实际问题。这是和贴近生活的问题。

当时做的不熟悉,所以要好好地学,在寒假掌握它!!加油!!

转载于:https://www.cnblogs.com/mitang0-0/p/10308210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值