public class TicketThread implements Runnable {
private int i = 5;
//我的理解是它是多个线程来共享它,不存在拷贝,修改以后必须马上写回。
public void run(){
while(i>0){
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i--;
System.out.println("1:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("2:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("3:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("4:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("5:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
TicketThread ticketThread = new TicketThread();
Thread t1 = new Thread(ticketThread);
Thread t2 = new Thread(ticketThread);
Thread t3 = new Thread(ticketThread);
Thread t4 = new Thread(ticketThread);
Thread t5 = new Thread(ticketThread);
Thread t6 = new Thread(ticketThread);
Thread t7 = new Thread(ticketThread);
Thread t8 = new Thread(ticketThread);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
t8.start();
}
}
1:Thread-0卖出车票,车票还剩 4 张
2:Thread-0卖出车票,车票还剩 3 张 //怎么会成了3张???
1:Thread-1卖出车票,车票还剩 3 张
2:Thread-1卖出车票,车票还剩 3 张
3:Thread-1卖出车票,车票还剩 3 张
4:Thread-1卖出车票,车票还剩 3 张
5:Thread-1卖出车票,车票还剩 3 张
1:Thread-2卖出车票,车票还剩 2 张
1:Thread-4卖出车票,车票还剩 1 张
2:Thread-4卖出车票,车票还剩 1 张
3:Thread-4卖出车票,车票还剩 1 张
4:Thread-4卖出车票,车票还剩 1 张
5:Thread-4卖出车票,车票还剩 0 张
3:Thread-0卖出车票,车票还剩 2 张
4:Thread-0卖出车票,车票还剩 0 张 //做为独立的线程,它内部存储的应该是3张,又不会再去读i一次。
5:Thread-0卖出车票,车票还剩 0 张
2:Thread-2卖出车票,车票还剩 1 张
1:Thread-3卖出车票,车票还剩 0 张
2:Thread-3卖出车票,车票还剩 0 张
3:Thread-3卖出车票,车票还剩 0 张
4:Thread-3卖出车票,车票还剩 0 张
5:Thread-3卖出车票,车票还剩 0 张
3:Thread-2卖出车票,车票还剩 0 张
4:Thread-2卖出车票,车票还剩 0 张
5:Thread-2卖出车票,车票还剩 0 张
private int i = 5;
//我的理解是它是多个线程来共享它,不存在拷贝,修改以后必须马上写回。
public void run(){
while(i>0){
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i--;
System.out.println("1:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("2:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("3:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("4:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("5:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
TicketThread ticketThread = new TicketThread();
Thread t1 = new Thread(ticketThread);
Thread t2 = new Thread(ticketThread);
Thread t3 = new Thread(ticketThread);
Thread t4 = new Thread(ticketThread);
Thread t5 = new Thread(ticketThread);
Thread t6 = new Thread(ticketThread);
Thread t7 = new Thread(ticketThread);
Thread t8 = new Thread(ticketThread);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
t8.start();
}
}
1:Thread-0卖出车票,车票还剩 4 张
2:Thread-0卖出车票,车票还剩 3 张 //怎么会成了3张???
1:Thread-1卖出车票,车票还剩 3 张
2:Thread-1卖出车票,车票还剩 3 张
3:Thread-1卖出车票,车票还剩 3 张
4:Thread-1卖出车票,车票还剩 3 张
5:Thread-1卖出车票,车票还剩 3 张
1:Thread-2卖出车票,车票还剩 2 张
1:Thread-4卖出车票,车票还剩 1 张
2:Thread-4卖出车票,车票还剩 1 张
3:Thread-4卖出车票,车票还剩 1 张
4:Thread-4卖出车票,车票还剩 1 张
5:Thread-4卖出车票,车票还剩 0 张
3:Thread-0卖出车票,车票还剩 2 张
4:Thread-0卖出车票,车票还剩 0 张 //做为独立的线程,它内部存储的应该是3张,又不会再去读i一次。
5:Thread-0卖出车票,车票还剩 0 张
2:Thread-2卖出车票,车票还剩 1 张
1:Thread-3卖出车票,车票还剩 0 张
2:Thread-3卖出车票,车票还剩 0 张
3:Thread-3卖出车票,车票还剩 0 张
4:Thread-3卖出车票,车票还剩 0 张
5:Thread-3卖出车票,车票还剩 0 张
3:Thread-2卖出车票,车票还剩 0 张
4:Thread-2卖出车票,车票还剩 0 张
5:Thread-2卖出车票,车票还剩 0 张
解决方案 »
- java byte范围大于127,socket传送给c#,c#中byte怎么获得?
- 配置ecplise日志发现错误
- 初学基础作业里一个抽象类构造方法的问题,我这个写不出来,所以也不能compile
- 关于枚举的一个问题
- 问一个有关Static Initialization的问题
- 静态字段的应用
- 如何把当前时间的格式转换成,例如 May 1, 2013
- JAVA2核心技术的代码,在JDK1.4中编译出现:
- 谁能帮我看看一个小的程序 (送高分)
- 在java中如何调用打开方式在新窗口中显示一文件,如显示一图片
- netbeans 环境下 程序打包后,运行没有显示
- 求助各位高手,我实在不行了,关于网页数据下载,有一万多页!
[email protected]
public synchronized void make() {
while(i>0){
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i = i+9999;
i = i- 9999;
i--;
System.out.println("1:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("2:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("3:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("4:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
System.out.println("5:"+Thread.currentThread().getName()+"卖出车票,车票还剩 "+i+" 张");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}然后在run中引用make方法就OK了。你在回去测试下。
Thread t1 = new Thread(new TestC());
Thread t2 = new Thread(new TestC());
Thread t3 = new Thread(new TestC());
...
才是各线程管各线程的int i可以在i--之前再插入sleep效果,可以很容易观察到数据数据上锁不上锁的效果区分