以下代码中线程什么时候休眠?
When does a thread sleeps in the following code?
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 行。其他操作是否也不止一行?
另外,我的回答是否正确。
guruthread1.sleep() 和 Thread.sleep() 都在这里做同样的事情——它们暂停了主线程。 guruthread1.sleep() 是一种误导性的调用,因为 sleep() 是 class Thread
的静态方法
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 行。其他操作是否也不止一行?
另外,我的回答是否正确。
guruthread1.sleep() 和 Thread.sleep() 都在这里做同样的事情——它们暂停了主线程。 guruthread1.sleep() 是一种误导性的调用,因为 sleep() 是 class Thread
的静态方法