多线程(2)
2016-05-23 16:21
288 查看
简单的多线程理解,一个简单的购票,创建4个线程,卖5张票.
package util;
public class Test{
public static void main(String[] args) {
new Test().new Ticket().start();
new Test().new Ticket().start();
new Test().new Ticket().start();
new Test().new Ticket().start();
}
class Ticket extends Thread{
private int tick = 5;
public void run() {
while(true){
if(tick > 0){
System.out.println(Thread.currentThread().getName()+"----"+ tick--);
}
}
}
}
}
打印结果:
Thread-0----5
Thread-1----5
Thread-2----5
Thread-1----4
Thread-0----4
Thread-0----3
Thread-3----5
Thread-3----4
Thread-3----3
Thread-3----2
Thread-3----1
Thread-1----3
Thread-2----4
Thread-2----3
Thread-2----2
Thread-2----1
Thread-1----2
Thread-1----1
Thread-0----2
Thread-0----1
发现每个线程都有5张票卖出,与现实不符,发现在内部类的是,变量tick没有定义静态,修改完之后,
package util;
public class Test{
private static int tick = 5;
public static void main(String[] args) {
new Test().new Ticket().start();
new Test().new Ticket().start();
new Test().new Ticket().start();
new Test().new Ticket().start();
}
class Ticket extends Thread{
public void run() {
while(true){
if(tick > 0){
System.out.println(Thread.currentThread().getName()+"----"+ tick--);
}
}
}
}
}
打印结果:
Thread-0----5
Thread-1----4
Thread-0----3
Thread-2----1
Thread-1----2
一般程勋中不推荐用静态变量,回收时间比较长.
推荐写法:
package util;
public class Test{
public static void main(String[] args) {
Ticket t= new Test().new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
class Ticket implements Runnable{
private int tick = 5;
public void run() {
while(true){
if(tick > 0){
System.out.println(Thread.currentThread().getName()+"----"+ tick--);
}
}
}
}
}
打印结果:
Thread-0----5
Thread-2----3
Thread-2----1
Thread-1----4
Thread-0----2
package util;
public class Test{
public static void main(String[] args) {
new Test().new Ticket().start();
new Test().new Ticket().start();
new Test().new Ticket().start();
new Test().new Ticket().start();
}
class Ticket extends Thread{
private int tick = 5;
public void run() {
while(true){
if(tick > 0){
System.out.println(Thread.currentThread().getName()+"----"+ tick--);
}
}
}
}
}
打印结果:
Thread-0----5
Thread-1----5
Thread-2----5
Thread-1----4
Thread-0----4
Thread-0----3
Thread-3----5
Thread-3----4
Thread-3----3
Thread-3----2
Thread-3----1
Thread-1----3
Thread-2----4
Thread-2----3
Thread-2----2
Thread-2----1
Thread-1----2
Thread-1----1
Thread-0----2
Thread-0----1
发现每个线程都有5张票卖出,与现实不符,发现在内部类的是,变量tick没有定义静态,修改完之后,
package util;
public class Test{
private static int tick = 5;
public static void main(String[] args) {
new Test().new Ticket().start();
new Test().new Ticket().start();
new Test().new Ticket().start();
new Test().new Ticket().start();
}
class Ticket extends Thread{
public void run() {
while(true){
if(tick > 0){
System.out.println(Thread.currentThread().getName()+"----"+ tick--);
}
}
}
}
}
打印结果:
Thread-0----5
Thread-1----4
Thread-0----3
Thread-2----1
Thread-1----2
一般程勋中不推荐用静态变量,回收时间比较长.
推荐写法:
package util;
public class Test{
public static void main(String[] args) {
Ticket t= new Test().new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
class Ticket implements Runnable{
private int tick = 5;
public void run() {
while(true){
if(tick > 0){
System.out.println(Thread.currentThread().getName()+"----"+ tick--);
}
}
}
}
}
打印结果:
Thread-0----5
Thread-2----3
Thread-2----1
Thread-1----4
Thread-0----2
相关文章推荐
- 转:Connection: close和Connection: keep-alive有什么区别?
- 训练3 习题5
- Java虚拟机1:什么是Java
- UIView 会显示超出frame的部分
- C/C++ 笔试、面试题目大汇总2
- 51nod 1270 数组的最大代价(dp好题)
- 设计模式之装饰着模式
- makefile(二)变量
- Java基础回顾 : String类的内存分析
- 解决教学平台上文件中存在无扩展名BUG的办法
- 转: 基于nginx的hls直播系统
- 递归读取目录文件下的所有文件
- 快速搭建Web环境 Angularjs + Express3 + Bootstrap3
- 【React Native】把现代web科技带给移动开发者(一)
- Number的使用
- Cocoa Touch框架
- StringBuffer实现字符串翻转
- IOS 使用protobuf
- 查找实验报告
- HDU 2571 命运(简单DP)