I'm not sure which one the Thread.sleep(1000) is referring to since both threads are running as well as the main thread.
I've tried searching up answers online but can't seem to find anything anywhere.
public class Practice {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
ob1.t.start();
ob2.t.start();
try {
Thread.sleep(1000);
} catch(InterruptedException a) {
System.out.println("Exception a caught");
}
}
}
A thread can not make any other thread sleep. Thread.sleep() will always make the current thread sleep. Even if you called ob1.t.sleep(), you would still be calling the static sleep(long millis) method via an instance of a Thread for the current thread you are in.
I repeat, you can not make any other thread sleep from a thread.
Yes, you are right when you say:
both threads are running as well as the main thread.
but what thread is the main method in? Is it ob1.t or ob2.t? No, it is the main thread, which means it will be the main thread that is sleeping.
In Java programs, the thread that runs the main(String[] args) function is the main thread. When you call the function Thread.sleep(long), the thread that will sleep is the thread that called the function. Take a look at this example below.
public class ThreadSleepTest {
public static void main(String[] args) {
new Thread(() -> {
System.out.println("oneSecondSleeper will sleep for 1 seconds");
try {
Thread.sleep(1000L);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
ex.printStackTrace();
}
System.out.println("oneSecondSleeper is awake now!");
}).start();
new Thread(() -> {
System.out.println("twoSecondSleeper will sleep for 2 seconds");
try {
Thread.sleep(2000L);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
ex.printStackTrace();
}
System.out.println("twoSecondSleeper is awake now!");
}).start();
System.out.println("mainThread will sleep for 3 seconds");
try {
Thread.sleep(3000L);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
ex.printStackTrace();
}
System.out.println("mainThread is awake now!");
}
}
The main thread will create two child threads that will sleep for one and two seconds respectively. After creating child threads, the main thread will sleep for 3 seconds.
Related
The message "main Thread" is printed before message "new Thread" though method with message "new Thread" is in syncronized section by object which include print* methods.
Test test = new Test();
new Thread(() -> {
try {
synchronized (test) {
Thread.sleep(5000);
test.printMessage();
}
} catch (InterruptedException e) {}
}).start();
Thread.sleep(1000);
test.printAnotherMessage();
}
public void printMessage() {
System.out.println("new Thread");
}
public void printAnotherMessage() {
System.out.println("main Thread");
}
}
In this example there is no synchronization between printAnotherMessage and the synchronized block that sleeps for 5 seconds, that's why main thread sleeps for 1 second and then prints main Thread without any waiting.
You probably intended to make printAnotherMessage a synchronized method.
In this case main thread will wait till the other thread finishes the execution of the block synchronized on test object.
There is no synchronisation on test.printAnotherMessage(); so it will be executed first assuming the timing is right. 4 seconds is a lot and should be enough.
synchronized (test) {
test.printAnotherMessage();
}
Thread.sleep is rarely a good option, though. A more proper way would be
Test test = new Test();
new Thread(() -> {
synchronized (test) {
test.printMessage();
test.notify();
}
}).start();
synchronized (test) {
test.wait();
test.printAnotherMessage();
}
I am playing a dangerous game here since I am assuming the main thread will enter the synchronized block and execute wait() before another thread is created and it enters its synchronized block. It's reasonable since creating a thread will take some time.
Test test = new Test();
new Thread(() -> {
try {
// a lot of time to let the main thread execute wait()
Thread.sleep(500);
synchronized (test) {
test.printMessage();
test.notify();
}
} catch (InterruptedException e) {}
}).start();
synchronized (test) {
test.wait();
test.printAnotherMessage();
}
public class Join1 {
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
System.out.print("EnterValue:");
try {
System.in.read();
} catch (Exception ex) {}
System.out.println("Thread Finished.");
}
};
System.out.println("Starting Thread");
t.start();
System.out.println("Joining");
try {
t.join();
} catch (Exception ex) {}
System.out.println("Main Finished.");
}
}
Output
Starting Thread
Joining
Enter Value:
Thread Finished
Main Finished
public class Join2 {
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
System.out.println("EnterValue:");
try {
System.in.read();
} // thread blocked
catch (Exception ex) {}
System.out.println("Thread Finished.");
}
};
System.out.println("Starting Thread");
t.start();
try {
Thread.sleep(2000);
} catch (Exception e) {}
System.out.println("No Join");
System.out.println("Main Finished.");
}
}
Output
Starting Thread
Enter Value:
No Join
Main Finished
3 (input)
Thread Finished
I don't understand the order of some of this output. Eg in Join2, why does it output the finished lines in main before you get to enter your value?
The only difference in the two examples given is the method invoked after the start of the thread with t.start()
Join1.java calls Thread.join() which, from the docs, says it "Waits for this thread to die". Thus only when the run method of the thread is finished (after the System.in.read() finishes) does the "Main Finished." print"
Join2.java calls Thread.sleep(2000) which pauses the thread for 2000 milliseconds. Try commenting that line out and seeing the result. Additionally, notice that the program did not quit after it prints "Main Finished." The thread is still waiting for input.
TLDR;
Thread.join() makes main pause until that thread finishes.
Thread.sleep(2000) only pauses main for 2 seconds before continuing, the other thread continues to run.
Think of threads as concurrent executions. By the time your main return from .start(), the new thread is free to proceed, ok. But in the Join2, you are not telling the main thread to wait for the thread t to be finished. So in 2 seconds it moves past the sleep.
I'd like to run a thread from the main thread , but I want this thread to be executed after the main thread is finished.
How should I do it ?
Can I pass the thread a reference to the main thread and call to join() method ?
The closest would be a shutdownHook,
Runtime.getRuntime().addShutdownHook(new Thread(){
...
});
But this would run will the process is still alive, once the process dies, thats it, you can't add any threads to a process that doesn't exist anymore.
You can use the Runtime.getRuntime() method for this. Here is an example:
public static void main(String[] args) {
.....
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
public void run(){
// run when the main thread finishes.
}
}));
}
You can find more details about this in the documentation
You can artificially do this with basic thread objects as follows, although I would recommend using the other answers on shutdown hooks instead.
public static void main(String[] args) throws Exception {
// parametrizes with current thread
new Deferrable(Thread.currentThread());
System.out.println("main thread");
}
static class Deferrable extends Thread {
Thread t;
Deferrable(Thread t) {
this.t = t;
// optional
// setDaemon(true);
start();
}
#Override
public void run() {
try {
// awaits termination of given Thread before commencing
t.join();
System.out.println("other thread");
} catch (InterruptedException ie) {
// TODO handle
}
};
}
This will always print:
main thread
other thread
The typical synchronizer for this situation is a CountDownLatch. Have the spawned thread wait for the CountDownLatch prior to doing what it needs to, and have the main thread finish by calling CountDownLatch.countDown()
public static void main(String[] args) {
final CountDownLatch latch = new CountDownLatch(1); // will need only one countDown to reach 0
new Thread(() -> {
try {
latch.await(); // will wait until CountDownLatch reaches 0
// do whatever is needed
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}).start();
// do stuff
// end main thread
latch.countDown(); // will make the latch reach 0, and the spawned thread will stop waiting
}
I wrote following Code :
public class ThreadDemo implements Runnable
{
private Thread t ;
private String threadName;
ThreadDemo(String threadName)
{
this.t = new Thread(this,threadName);
t.start();
}
public void run()
{
System.out.println("New thread has been started!!!" + t.getName());
}
public static void main(String args[])
{
new ThreadDemo("Thread-1");
Thread t = Thread.currentThread();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
new ThreadDemo("Thread-2");
}
}
So i have putted the join method on main thread . When i run it ,its execution never end.
Why it is so ? Why main thread doesn't end ? why it's running for infinite time.
The join() method waits for the thread that you call it on to finish. In your code, you are calling join() on the current thread - that is the same thread as you are calling it from. The main thread is now going to wait for itself to finish. That never happens, because it's waiting on itself...
You should not join the main thread, but the thread that you started instead.
ThreadDemo demo = new ThreadDemo("Thread-1");
try {
demo.t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Just another perspective to this code....
This code will hang even if you do not initialize the ThreadDemo objects
within the main program.
In short all this code can be reduced to saying the following statement,
Thread.currentThread().join() will never return.
I have a scenario where I have one thread that loops between waiting and executing a task. However, I would like to interrupt the wait for the thread (skip the rest of the wait if you will) and continue on to doing the task.
Anyone have any ideas how this could be done?
I think what you need is implement wait()/notify() ! check it out this tutorial: http://www.java-samples.com/showtutorial.php?tutorialid=306
There are a lot of them out there! if you need a more specific case, post a bit of your code!
cheers
You could use wait() and notify(). If your thread is waiting, you'll need to resume it by calling notify() from a different thread.
This is what Thread.interrupt is for:
import java.util.Date;
public class Test {
public static void main(String [] args) {
Thread t1 = new Thread(){
public void run(){
System.out.println(new Date());
try {
Thread.sleep(10000); // sleep for 10 seconds.
} catch (InterruptedException e) {
System.out.println("Sleep interrupted");
}
System.out.println(new Date());
}
};
t1.start();
try {
Thread.sleep(2000); // sleep for 2 seconds.
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}
Thread t1 will only sleep for 2 seconds, since the main thread interrupts it. Keep in mind that this will interrupt many blocking operations such as IO.