Skip to main content

Posts

Showing posts from November, 2012
    Importing Classes and Packages The first two lines of the following listing import two classes used in the applet: Applet and Graphics . import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } } If you removed the first two lines, the applet could still compile and run, but only if you changed the rest of the code like this: public class HelloWorld extends java.applet. Applet { public void paint( java.awt. Graphics g) { g.drawString("Hello world!", 50, 25); } } As you can see, importing the Applet and Graphics classes lets the program refer to them later without any prefixes. The java.applet. and java.awt. prefixes tell the compiler which packages it should search for the Applet and Graphics classes. Both the java.applet and java.awt packages are part of the core Java API -- API that every Ja
    Consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, 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 waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable. To avoid polling, Java includes an elegant interprocess communication mechanism via the following methods: wait( ): This method tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ). notify( ): This method wakes up the first thread that called wait( ) on the same object. notifyAll( ): This method wakes up all the threads that called wait( ) on the same object.c The hig

::::INTERTHREAD COMMUNICATION::::

    Inter-Thread Communication        Java provides a very efficient way through which multiple-threads can communicate with each-other. This way reduces the CPU?s idle time i.e. A process where, a thread is paused running in its critical region and another thread is allowed to enter (or lock) in the same critical section to be executed.  This technique is known as Interthread communication which is implemented by some methods. These methods are defined in " java.lang " package and can only be called  within synchronized code shown as:  Method  Description  wait( )  It indicates the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls method notify() or notifyAll() .  notify( )  It wakes up the first thread that called wait() on the same object.  notifyAll( )  Wakes up (Unloack) all the threads that cal

:::FANTASTIC EXP OF SCHRONIZATION:::::

          public class SynThread{ public static void main(String args[]){ Share s=new Share(); MyThread m1=new MyThread(s,"Thread1"); MyThread m2=new MyThread(s,"Thread2"); MyThread m3=new MyThread(s,"Thread3"); } } class MyThread extends Thread{ Share s; MyThread(Share s,String str){ super(str); this.s=s; start(); } public void run(){ s.doword(Thread.currentThread().getName()); } } class Share{ public synchronized void doword(String str){ for(int i=0;i<5;i++){ System.out.println("Started :"+str); try{ Thread.sleep(100); }catch(Exception e){} } } } Output of the program is: C:\j2se6\thread>javac SynThread.java C:\j2se6\thread>java SynThread Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread3 Started :Thread3 Started :Thread3 Started