比如:
public class Main
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread().start();
}
}
public class MyThread extends Thread
{
private static int tickets=100;
public void run(){
while(true){
tickets--;
if(tickets>0){
Thread th=Thread.currentThread();//获取当前线程
String na=th.getName();//获取当前线程名字
System.out.println(na+"正在发售第"+tickets+"张票");
}
}
}
}

java多线程可不可以通过私有静态变量创建多个对象开启线程实现资源共享?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 疯雪 2017-04-13 05:37关注
会导致**资源竞争**的出现,可以使用AtomicInteger来代替
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报