线程何时在以下代码中休眠?

时间:2018-12-16 17:48:03

标签: multithreading operating-system runnable thread-sleep

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行。其他操作也有不止一条线吗?

另外,我的答案正确吗?

1 个答案:

答案 0 :(得分:1)

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