多线程同步,在WIN 7下调试始终是一个线程在运行,为何?

2014我有一个梦想 2009-11-19 12:33:22

class ThreadDemo implements Runnable
{
private int ticket=10;
public synchronized void fun()
{
if(this.ticket>0)
{
try
{
//延迟操作,产生不同步的后果
Thread.sleep(100);
}
catch (Exception e)
{
}
System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
}
}
public void run()
{
while(ticket>0)
{
this.fun();
}
}
}

public class ch03
{
public static void main(String[] args)
{
ThreadDemo d=new ThreadDemo();

Thread t1=new Thread(d,"售票点A");
Thread t2=new Thread(d,"售票点B");
Thread t3=new Thread(d,"售票点C");

t1.start();
t2.start();
t3.start();
}
}


该段代码在WIN 7系统下只有卖票点B在出票,而代码原封不动的在XP系统下,A,B,C三个卖票点则是交替销售的。。。。为何如此啦?
JDK6.0
...全文
224 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 knightzhuwei 的回复:]
我也Win7 运行没有问题
可能是10太小了 你试着改成1000看看
[/Quote]
应该不是这里的问题,我改过。
knightzhuwei 2009-11-19
  • 打赏
  • 举报
回复
我也Win7 运行没有问题
可能是10太小了 你试着改成1000看看
heroboy0923 2009-11-19
  • 打赏
  • 举报
回复
公司只有xp,回去用win 7试试
sleep不会释放锁,去掉那行代码应该也没关系吧
bendanlzh 2009-11-19
  • 打赏
  • 举报
回复
有意思……
难道是线程A,C都给睡死了?呵呵,玩笑了……
等高人解答。
苍蝇①号 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 heroboy0923 的回复:]
引用 8 楼 w2j3z3j2h1 的回复:
和系统没有关系,是楼主设置的间隔时间太小!线程中的间隔时间是微秒,不是毫秒啊!再加上你这个多线程也是不安全的。。。。。。。。。


微秒?Are you sure?....
[/Quote]
是毫秒阿。难道这也有问题?翻翻API去
苍蝇①号 2009-11-19
  • 打赏
  • 举报
回复
时间设的太小了,加之thread调度的不确定性导致问题的发生
heroboy0923 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 w2j3z3j2h1 的回复:]
和系统没有关系,是楼主设置的间隔时间太小!线程中的间隔时间是微秒,不是毫秒啊!再加上你这个多线程也是不安全的。。。。。。。。。
[/Quote]

微秒?Are you sure?....
bendanlzh 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 w2j3z3j2h1 的回复:]
和系统没有关系,是楼主设置的间隔时间太小!线程中的间隔时间是微秒,不是毫秒啊!再加上你这个多线程也是不安全的。。。。。。。。。
[/Quote]
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds. The thread
* does not lose ownership of any monitors.
*
* @param millis the length of time to sleep in milliseconds.
* @exception InterruptedException if another thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
* @see java.lang.Object#notify()
*/
public static native void sleep(long millis) throws InterruptedException;

不知道8楼用的是哪个版本的java啊?
  • 打赏
  • 举报
回复
先不谈论线程的安全问题,sleep(100000)也是一样,但是将输出语句放到sleep之前就OK了
w2j3z3j2h1 2009-11-19
  • 打赏
  • 举报
回复
和系统没有关系,是楼主设置的间隔时间太小!线程中的间隔时间是微秒,不是毫秒啊!再加上你这个多线程也是不安全的。。。。。。。。。
  • 打赏
  • 举报
回复

class ThreadDemo implements Runnable
{
private int ticket=10;
public synchronized void fun()
{
if(this.ticket>0)
{
System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);

try
{
//延迟操作,产生不同步的后果
Thread.sleep(100);
}
catch (Exception e)
{
}
// System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
}
}
public void run()
{
while(ticket>0)
{
this.fun();
}
}
}
搞定了,将注释地方的代码放到红色字体位置,就OK了,但是不知道什么原因
  • 打赏
  • 举报
回复

class ThreadDemo implements Runnable
{
private int ticket=10;
public synchronized void fun()
{
if(this.ticket>0)
{
System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
try
{
//延迟操作,产生不同步的后果
Thread.sleep(100);
}
catch (Exception e)
{
}
// System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
}
}
public void run()
{
while(ticket>0)
{
this.fun();
}
}
}
搞定了,将注释地方的代码放到红色字体位置,就OK了,但是不知道什么原因。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧