Search code examples
multithreadingoperating-systemrunnablethread-sleep

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- } 

What's the difference between line 7 and line 20? is in both situation the thread will pause? Do they do the same task I mean?

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

Are there more lines? for example, in execution paused, we have 2 lines. Are other operations have also more than one line?

Also, are my answers correct.


Solution

  • guruthread1.sleep() and Thread.sleep() both do the same here - they pause the main thread. guruthread1.sleep() is a misleading invocation since sleep() is a static method of the class Thread