top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Example of Thread Sleep method in Java?

+1 vote
456 views
Example of Thread Sleep method in Java?
posted Apr 10, 2015 by Roshni

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Here is sample code example of Sleep Thread in Java.

In this example we have put Main thread in Sleep for 1 second.

/*
 * Example of Thread Sleep method in Java
 */
public class SleepTest {

       public static void main(String... args){
              System.out.println(Thread.currentThread().getName() + " is going to sleep for 1 Second");
              try {
                     Thread.currentThread().sleep(1000);
              } catch (InterruptedException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
              System.out.println("Main Thread is woken now");
       }

}

Output:

main is going to sleep for 1 Second

Main Thread is woken now

answer Apr 13, 2015 by Karthick.c
Similar Questions
+2 votes

You are given two processes and each process is having four threads. One of the thread is having performance issue. How will you find out that thread which is having problem.

0 votes

I heard the term "worker thread" and other types of threads such as "system thread" and "user thread".
For me a thread is nothing but a lite weight process and multi-threaded design is chosen for a software development when there are multiple task in the application/system and those tasks can be executed independently.
I want to know when these different terms are used like worker thread, user thread and system thread ?

+2 votes

Here my doubt is about acquire mutex lock.

Here pthread_mutex_t lock ; is also a global variable shared to threads. Accessing of this global variable (lock) will it be shame as accessing of other global variables ? If same, then don't we face same problem what we will face for other global variables ? if not ,how this is discriminated from other global variables ?

...