You are here:   HomeJava ProgrammingMultithreading
by Dinesh Thakur Category: Multithreading

The Java language and its run-time system was designed keeping in mind about multithreading. The run-time system depend upon multithreading. Java provides asynchronous thread environment, this helps to increase the utilization of CPU.



 
by Dinesh Thakur Category: Multithreading

If we talk about the mechanism of synchronization, then as one thread exits from the monitor then it must inform the waiting threads that it has left the monitor, now suspended thread can proceed to acquire the lock on the resources or entered in the monitor. If that is not possible then the waiting thread will always be in the waiting list. So, to solve this problem threads must communicate with each other.

 
by Dinesh Thakur Category: Multithreading

Another way to create a thread in Java is to define a class that implements Runnable interface. The previous technique of creating threads by extending the Thread class does not workwhen you want a class that extends another class and can also be run as a thread. This is becauseJava supports only single inheritance i.e. does not allow a class to extend more than one class. So insuch a case implement the Runnable interface. The Runnable interface declares only one method run () that contains the code that will be executed when the thread is started. Therefore, if youdefine a class that implements the Runnable interface then it must implement the run () method.



 
by Dinesh Thakur Category: Multithreading

 Priority allow the scheduler to take the decision when the thread should be allowed to run, and in which order. The higher priority threads get more CPU time then lower priority threads. A lower priority thread can be preempted by higher priority thread. The threads having equal priority get equal CPU time.

 
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

As any Java program comes under execution, first thread starts immediately, called the main thread. There are two importance's of main thread as follows:



 
by Dinesh Thakur Category: Multithreading

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

Multithreading is a powerful programming tool that makes it possible to achieve concurrent execution of multiple units of a program. Each portion of a program-designated a thread-may execute concurrently with others. Multithreading basically enables a program to do more than one task at a time and also to synchronize these tasks. Java builds thread support directly into the language. Multithreading support for Java includes thread creation, thread prioritizing, thread scheduling, resource locking (thread synchronization) and establishing inter-thread communication.



 

Page 1 of 5



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes 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.



What's New and Popular





Popular Article