Java-Get Main Thread Reference

Get Main Thread Reference When a Java program starts up, one thread begins running immediately. This is usually called the main thread of your program, because it is the one that is executed when your program begins. The main thread is created automatically when your program is started, it can be controlled through a Thread … Read more…

Java-IsAlive and Join Methods

IsAlive and Join Methods As mentioned, often you will want the main thread to finish last. In the following program, this is accomplished by calling sleep( ) within main( ), with a long enough delay to ensure that all child threads terminate prior to the main thread. Program However, this is hardly a satisfactory solution, … Read more…

Java-Stopping Thread

Stopping Thread Whenever we want to stop a thread from running further, we may do so by calling its stop( ) method. This method causes the thread to move to the dead state. A thread will also move to the dead state automatically when it reaches the end of its method. The stop( ) method … Read more…

Java-Thread Priority

Thread Priority We can assign different priorities to the threads defined in a program by using the setPriority( ) method. When we invoke this method, we have to pass a priority parameter to it. The priority parameter always takes a value between 0 and 10. One of the three static constants in the Thread class, … Read more…

Java-Synchronization

Synchronization So far, we have seen threads that use their own data and methods provided inside their run( ) methods. What happens when they try to use data and methods outside themselves? On such occasions, they may compete for the same resources and may lead to serious problems. For example, one thread may try to … Read more…

Java-Synchronization(Example Two)

Program : without Synchronization Program : with Synchronization Program Source : without Synchronization class TicketCounter{ int availableSeats = 2; void bookTicket(String pname,int numberOfSeats) { if((availableSeats>=numberOfSeats)&&(numberOfSeats>0)) { System.out.println(pname+” : “+numberOfSeats+” Seats Booking Success”); availableSeats-=numberOfSeats; }else System.out.println(pname+” : Seats Not Available”); } } class TicketBookingThread extends Thread{ TicketCounter tc; String name; int seats; TicketBookingThread(TicketCounter t,String pname,int pseats) { tc = … Read more…