Java-Command Line Arguments

Command Line Arguments The main() method also takes an argument. This is achieved by passing command line arguments is the information that directly follows the programs name on the command line when it is executed. To access the command line arguments inside a java program is very easy. They are stored in String array passed … Read more…

Java-Inter Thread Communication (Another Example)

Program Program Source class Data { boolean condition = false; synchronized void print_1_to_5() { condition = true; try{ for(int i=1;i<=5;i++) { Thread.sleep(1000); System.out.println(“Print_1_to_5 : “+i); } }catch(InterruptedException ie) { System.out.println(“Child Thread Interrupted”); } notify(); } synchronized void print_6_to_10() { if(!condition) { try{ System.out.println(“First thread called Print_6_to_10 method”); System.out.println(“So its waiting to second thread complete”); wait(); … Read more…

Java-Inter Thread Communication

Inter thread communication Inter-thread communication can be defined as the exchange of messages between two or more threads. The transfer of messages takes place before or after the change of state of a thread. For example, an active thread may notify to another suspended thread just before switching to the suspend state. The wait( ), … Read more…

Java-Synchronized Block

Synchronized Block Imagine that you want to synchronize access to objects of a class that was not designed for multithreaded access. That is, the class does not use synchronized methods. Further, this class was not created by you, but by a third party, and you do not have access to the source code. Thus, you … 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…

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…