Operations on Threads in Java

Today we will discuss about iperations on Threads (8.5.2). This is a lesson of Introduction to Programming Using Java.

Operations on Threads in Java

Operations on Threads

 

The Thread class includes several useful methods in addition to the start() method that was discussed above. I will mention just a few of them.

If third is an object of type Thread, then the boolean-valued function third.isAlive() can be used to test whether or not the thread is alive. A thread is “alive” between the time it is started and the time when it terminates. After the thread has terminated it is said to be “dead”. (The rather gruesome metaphor is also used when we refer to “killing” or “aborting” a thread.)

 

 

Chapter 8.5.2 - Operations on Threads | Introduction to Programming Using Java

 

 

The static method Thread.sleep(milliseconds) causes the thread that executes this method to “sleep” for the specified number of milliseconds. A sleeping thread is still alive, but it is not running. While a thread is sleeping, the computer will work on any other runnable threads – or on other programs. Thread.sleep() can be used to insert a pause in the execution of a thread.

The sleep method can throw an exception of type InterruptedException, which is an exception class that requires mandatory exception handling (see Subsection 8.3.4). In practice, this means that the sleep method is usually used in a try..catch statement that catches the potential InterruptedException:

23 4 Operations on Threads in Java

One thread can interrupt another thread to wake it up when it is sleeping or paused for some other reason. A Thread, third, can be interrupted by calling its method that.interrupt(), but you are not likely to do this until you start writing rather advanced applications, and you are not likely to need to do anything in response to an InterruptedException (except to catch it). It’s unfortunate that you have to worry about it at all, but that’s the way that mandatory exception handling works.

Sometimes, it’s necessary for one thread to wait for another thread to die. This is done with the join() method from the Thread class. Suppose that third is a Thread. Then, if another thread calls thrd. join(), that other thread will go to sleep until third terminates.

If thrd is already dead when third.join() is called, it simply has no effect— the thread called third.join() proceeds immediately. The method join() can throw an InterruptedException, which must be handled. As an example, the following code starts several threads, waits for them all to terminate, and then outputs the elapsed time:

Chapter 8.5.2 - Operations on Threads | Introduction to Programming Using Java

An observant reader will note that this code assumes that no InterruptedException will occur. To be absolutely sure that the thread worker[i] has terminated in an environment where InterruptedExceptions are possible, you would have to do something like:

Chapter 8.5.2 - Operations on Threads | Introduction to Programming Using Java

 

Source : Chapter 8.5.2 – Operations on Threads | Introduction to Programming Using Java

 

SEE MORE:

Leave a Comment