1.没办法改进,cpu的问题,把票数改大点就自然了
2.其实A不需要三个成员变量,一个就够了
package com.thread;import java.util.*;public class Test
{
public static void main(String[] args)
{
A aa1 = new A();
aa1.setName("Thread1");
aa1.start();
A aa2 = new A();
aa2.setName("Thread2");
aa2.start();
A aa3 = new A();
aa3.setName("Thread3");
aa3.start();
}
}
class A extends Thread
{
private static int ticket = 1000;
static String str = new String("qqq");
private int count = 0;
public void run()
{
while(true)
{
synchronized(str)
{
if(ticket > 0)
{
Date time = new Date();
//System.out.printf("%s is doing, and No.%d is sold at the time of %s.\n",
// Thread.currentThread().getName(), ticket, time);
ticket--;
count++;
}
else
{
break;
}
}
}
if(0 == ticket)
{
// System.out.printf("\ncount_0 = %d, count_1 = %d, count_2 = %d.\n",
// count_0, count_1, count_2);
System.out.print(Thread.currentThread().getName()+ " = "+ count + " ");
}
}
}
有个不明白的就是同步这个str是什么意思,请楼主解答下
2.其实A不需要三个成员变量,一个就够了
package com.thread;import java.util.*;public class Test
{
public static void main(String[] args)
{
A aa1 = new A();
aa1.setName("Thread1");
aa1.start();
A aa2 = new A();
aa2.setName("Thread2");
aa2.start();
A aa3 = new A();
aa3.setName("Thread3");
aa3.start();
}
}
class A extends Thread
{
private static int ticket = 1000;
static String str = new String("qqq");
private int count = 0;
public void run()
{
while(true)
{
synchronized(str)
{
if(ticket > 0)
{
Date time = new Date();
//System.out.printf("%s is doing, and No.%d is sold at the time of %s.\n",
// Thread.currentThread().getName(), ticket, time);
ticket--;
count++;
}
else
{
break;
}
}
}
if(0 == ticket)
{
// System.out.printf("\ncount_0 = %d, count_1 = %d, count_2 = %d.\n",
// count_0, count_1, count_2);
System.out.print(Thread.currentThread().getName()+ " = "+ count + " ");
}
}
}
有个不明白的就是同步这个str是什么意思,请楼主解答下
解决方案 »
- 编译原理NB的进,LR转LL
- jdk1.7环境变量设置问题,,
- 麻烦各位大虾帮我分析下下面的代码,就是运行程序后在界面画了几条线,然后把它最小化后又最大化,怎么原先画的几条线没有重绘出来
- jtable 熟悉的人进
- SOSS! 关于httpurlconnection 速度的问题
- 高手指导下啊,看不明白
- 请教RMI及对象的传递问题,急~~~~~~
- 刚从.net转战 jsp 请问java有没有像 msdn一样的类库的网站
- 我是JAVA初学者
- application server (websphere) webapp配置文件里里有这么一段,请问起什么作用, 对普通文件做目录映射吗?
- 请问如何使用7Z压缩整个文件夹
- 关于Java Swing panel中 图片保存(某区域)
如果没有这句,可能出现多个线程卖同一张票的情况
就是在类里面只定义一个成员变量就可以了啊,你的三个实例各自修改自己的成员变量不就能实现你要的功能么我只会用接口Runnable,然后再试着用一个变量,但是会出现每个站点都出1000张票的情况,可能我没有理解到您的意思,请您再再我根据您的提示所做的修改,请再次改正。import java.util.*;
public class Test_2
{
public static void main(String[] args)
{
A aa = new A();
Thread t1 = new Thread(aa);
t1.start();
Thread t2 = new Thread(aa);
t2.start();
Thread t3 = new Thread(aa);
t3.start();
}
}
class A implements Runnable
{
private static int ticket = 1000;
String str = new String("qqq");
private int count = 0;
public void run()
{
while(true)
{
synchronized(str)
{
if(ticket > 0)
{
Date time = new Date();
//System.out.printf("%s is doing, and No.%d is sold at the time of %s.\n",
// Thread.currentThread().getName(), ticket, time);
ticket--;
count++;
}
else
{
break;
}
}
}
if(0 == ticket)
{
// System.out.printf("\ncount_0 = %d, count_1 = %d, count_2 = %d.\n",
// count_0, count_1, count_2);
System.out.println(Thread.currentThread().getName()+ " = "+ count + " ");
}
}
}会出现都满票售出。
public class Test
{
public static void main(String[] args)
{
A aa = new A();
A bb = new A();
A cc = new A();
Thread t1 = new Thread(aa);
t1.start();
Thread t2 = new Thread(bb);
t2.start();
Thread t3 = new Thread(cc);
t3.start();
}
}
class A implements Runnable
{
private static int ticket = 1000;
static String str = new String("qqq");
private int count = 0;
public void run()
{
while(true)
{
synchronized(str)
{
if(ticket > 0)
{
Date time = new Date();
//System.out.printf("%s is doing, and No.%d is sold at the time of %s.\n",
// Thread.currentThread().getName(), ticket, time);
ticket--;
count++;
}
else
{
break;
}
}
}
// System.out.printf("\ncount_0 = %d, count_1 = %d, count_2 = %d.\n",
// count_0, count_1, count_2);
System.out.println(Thread.currentThread().getName()+ " = "+ count + " ");
System.out.println(ticket);
}
}