【问题标题】:When does a thread sleeps in the following code?下面的代码中线程什么时候休眠?
【发布时间】:2018-12-16 17:48:03
【问题描述】:
1- package demotest;
2- public class thread_example1 implements Runnable {
3- @Override
4- public void run() {
5- }
6- public static void main(String[] args) {
7-     Thread guruthread1 = new Thread();
8-     guruthread1.start();
9-     try {
10-         guruthread1.sleep(1000);
11-     }   catch (InterruptedException e) {
12-     // TODO Auto-generated catch block
13-         e.printStackTrace();
14-     }
15-     guruthread1.setPriority(1);
16-     int gurupriority = guruthread1.getPriority();
17-     System.out.println(gurupriority);
18-     System.out.println("Thread Running");
19-     Process proc = rt.exec("mspaint");
20-     Thread.sleep(5000);
21-     proc.destroy();
22- }
23- } 

第 7 行和第 20 行有什么区别?在这两种情况下线程都会暂停吗?他们做的任务和我说的一样吗?

I have this schedule :
Operation                          Line Number
Execution Paused                   10 and 20
Start Execution                    8
Closing Child Process              21
Thread Creation                    7
Execution of Child Process         19
Adjusting Priority                 15
Reading Priority                   16

还有更多的行吗?例如,在执行暂停时,我们有 2 行。难道其他操作也不止一条线?

另外,我的答案是否正确。

【问题讨论】:

    标签: multithreading operating-system runnable thread-sleep


    【解决方案1】:

    guruthread1.sleep() 和 Thread.sleep() 在这里都做同样的事情——它们暂停主线程。 guruthread1.sleep() 是一个误导性的调用,因为 sleep() 是类 Thread 的静态方法

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 2014-09-14
      相关资源
      最近更新 更多