永发信息网

关于JAVA线程的问题

答案:1  悬赏:80  手机版
解决时间 2021-05-09 17:42
  • 提问者网友:眉目添风霜
  • 2021-05-09 04:37
class ThreadDemo1{
 public static void main(String [] args) {  TestThread t = new TestThread();  new Thread(t).start();  new Thread(t).start();  new Thread(t).start();  new Thread(t).start(); }}class TestThread implements Runnable{ String str = new String(""); int ticket =100;  public void run() {  synchronized(str)  {  while(true)  {    if(ticket>0)    {     try{ Thread.sleep(10);}  catch(Exception e)  {  }     System.out.println(Thread.currentThread().getName()+"is saling"+ticket--);    }  }  } }}我也不知道怎么说这个问题,就是书上运行结果是:Thread-1 is saling tickets 100Thread-2 is saling tickets 99Thread-3 is saling tickets 98Thread-4 is saling tickets 97Thread-1 is saling tickets 96Thread-2 is saling tickets 95Thread-3 is saling tickets 94Thread-4 is saling tickets 93         .                                               .                                               .                                               .                                      Thread-1 is saling tickets 4Thread-2 is saling tickets 3Thread-3 is saling tickets 2Thread-4 is saling tickets 1而我的执行结果(多次基本上都是这个结果):Thread-0 is saling tickets 100Thread-0 is saling tickets 99Thread-0 is saling tickets 98Thread-0 is saling tickets 97Thread-0 is saling tickets 96Thread-0 is saling tickets 95Thread-0 is saling tickets 94Thread-0 is saling tickets 93         .                                      .         .                                      .         .                                      .         .                                      .Thread-0 is saling tickets 4Thread-0 is saling tickets 3Thread-0 is saling tickets 2Thread-0 is saling tickets 1有几点疑问:(1)书上的是从Thread-1开始的,而我的却是从Thread-0开始的,不知道这跟系统或者配置有关吗?(2)书上这个是交替运行,我这个却不是,Thread-0这个进程一直不让出cpu,不懂为什么???(3)后来猜测是不是由于处理机,cpu等等都比书上那时候快得多,于是把sleep时间调到了sleep(19),运行多次虽然还是多次出现:Thread-0 is saling tickets 100Thread-0 is saling tickets 99Thread-0 is saling tickets 98Thread-0 is saling tickets 97Thread-0 is saling tickets 96Thread-0 is saling tickets 95Thread-0 is saling tickets 94Thread-0 is saling tickets 93         .                                               .                                               .                                               .                                      Thread-0 is saling tickets 4Thread-0 is saling tickets 3Thread-0 is saling tickets 2Thread-0 is saling tickets 1这个结果,但也偶尔出现了如下结果:Thread-0 is saling tickets 100Thread-0 is saling tickets 99Thread-0 is saling tickets 98Thread-0 is saling tickets 97Thread-0 is saling tickets 96         .                                               .                                               .                                               . Thread-0 is saling tickets 73Thread-3 is saling tickets 72Thread-3 is saling tickets 71Thread-3 is saling tickets 70Thread-3 is saling tickets 69Thread-3 is saling tickets 68Thread-3 is saling tickets 67Thread-3 is saling tickets 66Thread-1 is saling tickets 65Thread-1 is saling tickets 64Thread-1 is saling tickets 63         .                                               .                                               .                                               .Thread-1 is saling tickets 1虽然这次出现了其它进程,应该算是交替执行吧??可是还是不是书上那种,顺序 执行!!~(4)sleep后一定会出现cpu切换进程吗?或者说cpu不一定切换,而在那个进程那里等他睡眠结束?
最佳答案
  • 五星知识达人网友:动情书生
  • 2021-05-09 04:51

书上是错的


线程名默认是从Thread-0开始


并且运行的是同一个对象实例,只要有一个实例调用了synchronized(str),在没有释放之前,其它实例不可能会被执行,不管里面sleep了多少秒,只要锁还没有结束,其它实例想再调用str的锁,那是一定要等待的。


你说的sleep的值调大一点,就会出现其它情况,那不可能,不管多少秒结果都是一定的。你可以在if后面那里加个else,会看到其它线程其实也进入循环了,只是那时候的ticket已经是小于0,所以它们都没有输出


我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯