Reputation: 329
I just started with the multithreading concepts of Java. I have written a small Java program but however, I am really not able to understand its behavior.
public class Mythread implements Runnable{
@Override
public void run() {
System.out.println("mythread: ");
Thread t=new Thread(this,"thread1");
for(int i=1;i<5;i++)
{
System.out.println("in for of myThread");
try {
t.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class ThreadTest {
public static void main(String[] args) {
System.out.println("in main thread");
Mythread mythread=new Mythread();
Thread thread=new Thread(mythread,"thread0");
thread.start();
for(int i=1;i<5;i++)
{
System.out.println("main class: "+i);
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Now when I execute the above program I see that when thread1 goes to sleep thread0 also goes to sleep.
t.sleep(1000);
Are thread1
and thread0
the same thread?
Also, I haven't started thread1
anywhere in my code then why does the thread go to sleep?
I am just a beginner to multithreading and referring Java The Complete reference book.
Upvotes: 3
Views: 8068
Reputation: 46
No, thread0
and thread1
are not the same thread. As said in previous answers, sleep method is static, it will put current thread to sleep. Since thread1 has never been to runnable state, there's no concept of it going in sleep state, it is your thread0 which is going to sleep.
For further reference on this you can refer here to get clear insights on the flow of the states of thread and creation of new thread from another thread.
Upvotes: 0
Reputation: 718836
The Thread.sleep(...)
method causes the current thread to sleep. It is a static method, not an instance method.
There is no safe way for one thread to force another thread to sleep.
You are also making other mistakes:
Creating a subclass of Thread
is usually a mistake. The recommended way to do threading is to write a class that implements Runnable
, and create instances of java.lang.Thread
as your threads. Better still, use either a thread pool, fork-join pool or an ExecutorService
instance.
In this:
Mythread mythread = new Mythread();
Thread thread = new Thread(mythread, "thread0");
you are actually using the mythread
objects as (only) a Runnable
.
The threads that you created in the run()
method are never used ... because you never start them. But if you did, there would be a thread explosion ... because you are instantiating them with this
as the Runnable
, and the run()
method just creates more threads.
There are a number of Java style violations ... starting with your choice of Mythread
as a classname.
To answer your questions:
is thread1 and thread0 refers to same thread ?
No.
Also I haven't started thread1 anywhere in my code then why thread goes to sleep ?
In fact, it is thread0 that is going to sleep. If you change
System.out.println("in for of myThread");
to
System.out.println("in for of myThread: " + Thread.currentThread());
you will see ...
Upvotes: 3
Reputation: 3563
Thread.sleep
is a class method, not an instance method. Effectively you are calling Tread.sleep(1000)
, which is a request on the threading classes to have the currently executing thread to sleep.
So although thread1
and thread2
are not the same thread, each thread requests to sleep for 1 second.
btw. any good IDE will tell you 'The static method sleep(long) from the type Thread should be accessed in a static way".
Upvotes: 1