public class DeadlockDemo
{
String s1 = "Window";
String s2 = "Linux";
Thread t1 = new Thread(" Thread 1")
{
public void run()
{
while(true)
{
synchronized(s1)
{
synchronized(s2)
{
System.out.println(s1 + s2);
}
}
}
}
};
Thread t2 = new Thread(" Thread 2")
{
public void run()
{
while(true)
{
synchronized(s2)
{
synchronized(s1)
{
System.out.println(s2 + s1);
}
}
}
}
};
public static void main(String a[])
{
DeadlockDemo dd = new DeadlockDemo();
dd.t1.start();
dd.t2.start();
}
}
public class InterThreadDemo implements Runnable
{
public void run ()
{
synchronized (InterThreadDemo.class)
{
System.out.println ("Waiting: " + this);
try
{
InterThreadDemo.class.wait ();
}
catch (InterruptedException ex)
{
return;
}
System.out.println ("Notified: " + this);
}
}
public static void main (String [] args) throws Exception
{
for (int i = 0; i < 10; i++)
new Thread (new InterThreadDemo ()).start ();
Thread.sleep (100);
System.out.println ("notify () method ");
synchronized (InterThreadDemo.class)
{
InterThreadDemo.class.notify ();
}
Thread.sleep (100);
System.out.println ("notifyAll () method ");
synchronized (InterThreadDemo.class)
{
InterThreadDemo.class.notifyAll ();
}
}
}