Monday, April 21, 2008

MULTITHREADING

JOIN IAS ALIVE SLEEP GETNAME


class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this.name);
System.out.println("New Thread:"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println(name+":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(name+"Interrupted");
}
System.out.println(name+"Exiting");
}
}
class DemoJoin
{
public static void main(String args[])
{
NewThread ob1=new NewThread("one");
NewThread ob2=new NewThread("two");
NewThread ob3=new NewThread("three");
System.out.println("Thread one is alive:"+ob1.t.isAlive());
System.out.println("Thread two is alive:"+ob2.t.isAlive());
System.out.println("Thread three is alive:"+ob3.t.isAlive());
System.ot.println("name of the thread is"+ob1.t.getName());
try
{
System.out.println("Waiting for threads to finish");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Thread one is alive:"+ob1.t.isAlive());
System.out.println("Thread two is alive:"+ob2.t.isAlive());
System.out.println("Thread three is alive:"+ob3.t.isAlive());
System.out.println("Main thread exiting");
}
}
}




--------------------------------------------------------------
MULTITHREAD
class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this.name);
System.out.println("New thread"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
System.out.println(name+":"+i);
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(name+"Interrupted");
}
System.out.println("Exiting"+name);
}
}
class MultiThreadDemo
{
public static void main(String args[])
{
new NewThread("one");
new NewThread("two");
new NewThread("Three");
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
System.out.println("Main Thread interrupted");
System.out.println("Main thread exited");
}
}

}

------------------------------------------------------------
PRODUCER CONSUMER INTRER THREAD COMMUNICATION
class Q
{
int n;
boolean v=false;
synchronized int get()
{
if(!v)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Exception caught");
}
System.out.println("got"+n);
v=false;
notify();
return n;
}
synchronized void put(int x)
{
if(v)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Exception caught");
}
}
n=x;
v=true;
System.out.println("put"+n);
notify();
}
}

class Producer implements Runnable
{
Q q;
Producer(Q q1)
{
q=q1;
new Thread(this,"Producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q;
Consumer(Q q1)
{
q=q1;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class PC
{
public static void main(String arg[])
{
Q q=new Q();
new Producer(q);
new Consumer(q);
}
}

-----------------------------------------------------------------------
SYNCHRONIZATION
class Sync
{
void printMsg(String msg)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(""+e);
}
System.out.println("J");
}
}
class SDemo extends Thread
{
Sync s;
String m;
SDemo(Sync s1,String msg1)
{
s=s1;
m=msg1;
start();
}
public void run()
{
s.printMsg(m);
}
}
class SynDemo
{
public static void main(String a[])
{
Sync s1=new Sync();
SDemo t1=new SDemo(s1,"First");
SDemo t2=new SDemo(s1,"Second");
try
{
t1.join();
t2.join();
}
catch(InterruptedException e)
{
System.out.println("Main Interrupted");
}
System.out.println("Main Exited");
}
}

-----------------------------------------------------------
THREAD PRIORITIES

lass A extends Thread
{
public void run()
{
for(int i=1;i<4;i++)
{
System.out.println("A"+i);
}
}
}
class B extends Thread
{
public void run()
{
for(int i=5;i<10;i++)
{
System.out.println("B"+i);
}
}
}
class C extends Thread
{
public void run()
{
for(int i=20;i<25;i++)
{
System.out.println("A"+i);
}
}
}
class ThreadPriority
{
public static void main(String a[])
{
A t1=new A();
B t2=new B();
C t3=new C();
t3.setPriority(10);
System.out.println("t3...."+t3);
t2.setPriority(t2.getPriority()+1);
System.out.println("t2...."+t2);
t1.setPriority(Thread.MIN_PRIORITY);
System.out.println("t1...."+t1);
t1.start();
t2.start();
t3.start();
}
}

No comments: