by Dinesh Thakur Category: Multithreading

When more than one thread has to use a shared resource, Java finds a way of ensuring that only one thread uses the resources at one point of time; this is called synchronization.

 
by Dinesh Thakur Category: Multithreading

A thread can undergo some states during its life cycle. It is because in a multithreaded environment when multiple threads are executing only one thread can use the CPU at a time, and all other threads should be in some other states either waiting for their turn for the CPU or waiting for some other condition to be satisfied.

A thread is always in one of five states: newborn, runnable, running, dead and blocked. Figure shows the life cycle of a thread.

 
by Dinesh Thakur Category: Multithreading

In Java, threads are objects and can be created in two ways:

1. by extending the class Thread

2. by implementing the interface Runnable

 
 
by Dinesh Thakur Category: Multithreading
 
by Dinesh Thakur Category: Multithreading

Multithreading replaces event loop programming by dividing the tasks into discrete and logical units. Threads also helps in avoiding polling. Polling is usually implemented by a loop that is used to check some condition repeatedly. Once the condition is true, appropriate action is taken, this results in wastage of CPU time. Suppose that the producer has to wait until the consumer is finished before it generates more data. In a polling system, the consumer would waste many CPU cycles while it waits for the producer to produce. Once the producer has finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on.

 
by Dinesh Thakur Category: Multithreading

Since multiple threads run independently without any communication between them, some problems may arise because of this asynchronous behavior of threads. Sometimes, we need an operation to be performed by only one thread at a time. Performing the same task by two threads simultaneously may lead to inconsistent results. So to ensure that two thread don't execute the conflicting operations together and to initiate communication among thread, a process called synchronization is used.

 
by Dinesh Thakur Category: Multithreading

Thread priorities are used by the thread scheduler to decide when each thread should be allowed to run. Higher priority threads get more CPU time than lower priority threads. A higher-priority thread can also preempt a lower-priority one. For instance, when a lower-priority thread is running and a higher-priority thread resumes (from sleeping or waiting on I/O, for example), it will preempt the lower priority thread.

 
 
by Dinesh Thakur Category: Multithreading

To determine whether a thread has finished. we can call isAlive() on the thread. This method is defined by Thread and its syntax is :

final boolean isAlive()

This method returns true if the thread upon which it is called is still running otherwise it returns false.

 
by Dinesh Thakur Category: Multithreading

We first make a class MultipleThreads which extends Thread class and then we create an instance of that class t1 and pass the string "Child Thread" as parameter to it which assigns the name of the thread as Child Thread and also displays the information of the child thread on the screen. After that the start() method is invoked on that object which initiates the run() method. Before executing run() method of MultipleThreads, control returns to main(). In main() method, the for loop is executed which is supposed to print Mainthread : 1



 
by Dinesh Thakur Category: Multithreading

Another method to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. We can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run().

 
by Dinesh Thakur Category: Multithreading

One way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run() method, which is the entry point for the new thread. It must also call start() to begin execution of the new thread.

 
by Dinesh Thakur Category: Multithreading

In this Java Example, a reference to the current thread (the main thread) is obtained by calling currentThread(), and this reference is stored in a local variable t. Then, we display the information about the thread. After that, a loop counts from 0 to 10, pausing one second between each line. The pause is accomplished by the sleep() method. The argument to sleep() specifies the delay period in milliseconds. The sleep() method might throw an InterruptedException if some other thread interrupts the sleeping one.



 

Page 1 of 2



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles