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

Every thread in Java has a priority that helps the thread scheduler to determine the order in which threads are scheduled. The threads with higher priority will usually run before and/or more frequently than lower priority threads. By default, all the threads had the same priority i.e. they are regarded as being equally important by the scheduler, when a thread is created it inherits its priority from the thread that created it. However, you can explicitly set a thread's priority at any time after its creation by calling its setPriority () method. This method accepts an argument of type in t that defines the new priority of the thread. Its

 
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

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

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

In this Example, we shows how the constructors and methods of the Thread class are used. Here, we have created a subclass MyThread that extends Thread class. This class consists of a field which stores the time in milliseconds for which the thread will sleep. It also contains a parameterized constructor that contains two parameters str of String type and d of int type. This constructor calls the superclass constructor that sets the Thread's name to the value passed in str and delay field is set to value passed in d.

 
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

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.

 

Page 1 of 5

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.



Search Content







Popular Article