Monday, April 28, 2008

thread program

/ without join
class MyThread extends Thread
{
MyThread(String s)
{
super(s); // to create a new Thread with s name
start();
}
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(getName()+" Thread here");
try{ sleep(1000); }catch(Exception ex){}
}
}
}
class My1
{
public static void main(String aa[])
{
MyThread t1 = new MyThread("Ankit");
MyThread t2 = new MyThread("Hari Om");
MyThread t3 = new MyThread("Gaurav");
MyThread t4 = new MyThread("Prateek");
System.out.println("main Thread exit ................");
}
}




// without join
class MyThread extends Thread
{
MyThread(String s)
{
super(s); // to create a new Thread with s name
start();
}
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(getName()+" Thread here");
try{ sleep(1000); }catch(Exception ex){}
}
}
}
class My2
{
public static void main(String aa[])
{
MyThread t1 = new MyThread("Ankit");
t1.setPriority(2); // Thread.MIN_PRIORITY+1 /Thread.NORM_PRIORITY-3 / Thread.MAX_PRIORITY-8 /
MyThread t2 = new MyThread("Hari Om");
t2.setPriority(4);
MyThread t3 = new MyThread("Gaurav");
t3.setPriority(9);
MyThread t4 = new MyThread("Prateek");
try
{
t1.join();
t2.join();
t3.join();
t4.join();
}catch(Exception ex){}
System.out.println("main Thread exit ................");
}
}







// without join
class MyThread extends Thread
{
AbhiTask t;
MyThread(String s,AbhiTask a)
{
super(s); // to create a new Thread with s name
t = a;
start();
}
public void run()
{
t.work(getName());
}
}
class AbhiTask
{
void work(String s)
{
for(int i=1;i<=5;i++)
{
System.out.println(s+" Thread here");
try{ Thread.sleep(1000); }catch(Exception ex){}
}
}
}
class Syn1
{
public static void main(String aa[])
{
AbhiTask t = new AbhiTask();
MyThread t1 = new MyThread("Ankit",t);
MyThread t2 = new MyThread("Hari Om",t);
MyThread t3 = new MyThread("Gaurav",t);
MyThread t4 = new MyThread("Prateek",t);
try
{
t1.join();
t2.join();
t3.join();
t4.join();
}catch(Exception ex){}
System.out.println("main Thread exit ................");
}
}



// with method synchronization - single method to be synchronized
class MyThread extends Thread
{
AbhiTask t;
MyThread(String s,AbhiTask a)
{
super(s); // to create a new Thread with s name
t = a;
start();
}
public void run()
{
t.work(getName());
}
}
class AbhiTask
{
synchronized void work(String s)
{
for(int i=1;i<=5;i++)
{
System.out.println(s+" Thread here");
try{ Thread.sleep(1000); }catch(Exception ex){}
}
}
}
class Syn2
{
public static void main(String aa[])
{
AbhiTask t = new AbhiTask();
MyThread t1 = new MyThread("Ankit",t);
MyThread t2 = new MyThread("Hari Om",t);
MyThread t3 = new MyThread("Gaurav",t);
MyThread t4 = new MyThread("Prateek",t);
try
{
t1.join();
t2.join();
t3.join();
t4.join();
}catch(Exception ex){}
System.out.println("main Thread exit ................");
}
}







// with Object synchronization - all methods to be synchronized
class MyThread extends Thread
{
AbhiTask t;
MyThread(String s,AbhiTask a)
{
super(s); // to create a new Thread with s name
t = a;
start();
}
public void run()
{
synchronized(t) // object synchronized
{
t.work(getName());
}
}
}
class AbhiTask
{
void work(String s)
{
for(int i=1;i<=5;i++)
{
System.out.println(s+" Thread here");
try{ Thread.sleep(1000); }catch(Exception ex){}
}
}
}
class Syn3
{
public static void main(String aa[])
{
AbhiTask t = new AbhiTask();
MyThread t1 = new MyThread("Ankit",t);
MyThread t2 = new MyThread("Hari Om",t);
MyThread t3 = new MyThread("Gaurav",t);
MyThread t4 = new MyThread("Prateek",t);
try
{
t1.join();
t2.join();
t3.join();
t4.join();
}catch(Exception ex){}
System.out.println("main Thread exit ................");
}
}




// with Object synchronization - all methods to be synchronized
class MyThread extends Thread
{
AbhiTask t;
MyThread(String s,AbhiTask a)
{
super(s); // to create a new Thread with s name
t = a;
start();
}
public void run()
{
synchronized(t) // object synchronized
{
t.work(getName());
t.work1(getName());
}
}
}
class AbhiTask
{
void work(String s)
{
for(int i=1;i<=5;i++)
{
System.out.println(s+" Thread here");
try{ Thread.sleep(1000); }catch(Exception ex){}
}
}
void work1(String s)
{
for(int i=1;i<=5;i++)
{
System.out.println(s);
try{ Thread.sleep(500); }catch(Exception ex){}
}
}
}
class Syn4
{
public static void main(String aa[])
{
AbhiTask t = new AbhiTask();
MyThread t1 = new MyThread("Ankit",t);
MyThread t2 = new MyThread("Hari Om",t);
MyThread t3 = new MyThread("Gaurav",t);
MyThread t4 = new MyThread("Prateek",t);
try
{
t1.join();
t2.join();
t3.join();
t4.join();
}catch(Exception ex){}
System.out.println("main Thread exit ................");
}
}







// with method synchronization -single method to be synchronized
class MyThread extends Thread
{
AbhiTask t;
MyThread(String s,AbhiTask a)
{
super(s); // to create a new Thread with s name
t = a;
start();
}
public void run()
{
t.work(getName());
t.work1(getName());
}
}
class AbhiTask
{
synchronized void work(String s)
{
for(int i=1;i<=5;i++)
{
System.out.println(s+" Thread here");
try{ Thread.sleep(1000); }catch(Exception ex){}
}
}
synchronized void work1(String s)
{
for(int i=1;i<=5;i++)
{
System.out.println(s);
try{ Thread.sleep(500); }catch(Exception ex){}
}
}
}
class Syn5
{
public static void main(String aa[])
{
AbhiTask t = new AbhiTask();
MyThread t1 = new MyThread("Ankit",t);
MyThread t2 = new MyThread("Hari Om",t);
MyThread t3 = new MyThread("Gaurav",t);
MyThread t4 = new MyThread("Prateek",t);
try
{
t1.join();
t2.join();
t3.join();
t4.join();
}catch(Exception ex){}
System.out.println("main Thread exit ................");
}
}







// to control the main Thread
class Th1
{
public static void main(String aa[])
{
for(char c='A';c<='Z';c++)
{
System.out.println(c);
try{ Thread.sleep(500); }catch(Exception ex){}
}
}
}





// to control the main Thread
import java.util.*;
class Th2
{
public static void main(String aa[])
{
Date d;
while(true)
{
d = new Date();
System.out.println(d.getHours()+" : "+d.getMinutes()+" : "+d.getSeconds());
try{ Thread.sleep(1000); }catch(Exception ex){}
}
}
}






// to create own thread
import java.util.*;
import java.applet.*;
import java.awt.*;
public class Th3 extends Applet implements Runnable
{
Button b = new Button("Clock");
Thread t;
Date d;
public void init()
{
setLayout(null);
b.setBounds(50,100,600,140);
b.setFont(new Font("Courier New",1,100));
add(b);
t = new Thread(this); // to create the new Thread for current applet
t.start();
}
public void run()
{
while(true)
{
d = new Date();
b.setLabel(d.getHours()+" : "+d.getMinutes()+" : "+d.getSeconds());
try{ t.sleep(1000); }catch(Exception ex){}
}
}
}
//








// to create own thread
import java.util.*;
import java.applet.*;
import java.awt.*;
public class Th4 extends Applet implements Runnable
{
Button b = new Button("Clock");
Thread t;
Date d;
public void init()
{
b.setForeground(Color.white);
setLayout(null);
b.setBounds(50,100,600,140);
b.setFont(new Font("Courier New",1,100));
add(b);
t = new Thread(this); // to create the new Thread for current applet
t.start();
new AbhiThread().start(); // AbhiThread t1 = new AbhiThread(); t1.start();
}
public void run()
{
while(true)
{
d = new Date();
b.setLabel(d.getHours()+" : "+d.getMinutes()+" : "+d.getSeconds());
try{ t.sleep(1000); }catch(Exception ex){}
}
}
class AbhiThread extends Thread
{
int cr,cg,cb;
public void run()
{
while(true)
{
cr = (int)(Math.random()*205);
cg = (int)(Math.random()*205);
cb = (int)(Math.random()*205);
b.setBackground(new Color(cr,cg,cb));
setBackground(new Color(cg,cb,cr));
try{ t.sleep(5000); }catch(Exception ex){}
}
}
}
}
//






// to create own thread
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Th5 extends Applet implements Runnable,ActionListener
{
boolean flag = true;
Button b = new Button("Clock");
Thread t;
Date d;
public void init()
{
b.setForeground(Color.white);
setLayout(null);
b.setBounds(50,100,600,140);
b.setFont(new Font("Courier New",1,100));
add(b);
t = new Thread(this); // to create the new Thread for current applet
t.start();
new AbhiThread().start(); // AbhiThread t1 = new AbhiThread(); t1.start();
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(flag)
{
t.suspend();
flag = false;
}
else
{
t.resume();
flag = true;
}
}
public void run()
{
while(true)
{
d = new Date();
b.setLabel(d.getHours()+" : "+d.getMinutes()+" : "+d.getSeconds());
try{ t.sleep(1000); }catch(Exception ex){}
}
}
class AbhiThread extends Thread
{
int cr,cg,cb;
public void run()
{
while(true)
{
cr = (int)(Math.random()*205);
cg = (int)(Math.random()*205);
cb = (int)(Math.random()*205);
b.setBackground(new Color(cr,cg,cb));
setBackground(new Color(cg,cb,cr));
try{ t.sleep(5000); }catch(Exception ex){}
}
}
}
}
//






// to create own thread
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Th6 extends Applet implements Runnable,ActionListener
{
boolean flag = true;
Button b = new Button("Clock");
Thread t;
int m=59,s=59;
public void init()
{
b.setForeground(Color.white);
setLayout(null);
b.setBounds(50,100,600,140);
b.setFont(new Font("Courier New",1,100));
add(b);
t = new Thread(this); // to create the new Thread for current applet
t.start();
new AbhiThread().start(); // AbhiThread t1 = new AbhiThread(); t1.start();
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(flag)
{
t.suspend();
flag = false;
}
else
{
t.resume();
flag = true;
}
}
public void run()
{
while(m>=0)
{
s--;
if(s<=0)
{
m--;
s = 59;
}
b.setLabel("00 : "+m+" : "+s);
try{ t.sleep(1000); }catch(Exception ex){}
}
}
class AbhiThread extends Thread
{
int cr,cg,cb;
public void run()
{
while(true)
{
cr = (int)(Math.random()*205);
cg = (int)(Math.random()*205);
cb = (int)(Math.random()*205);
b.setBackground(new Color(cr,cg,cb));
setBackground(new Color(cg,cb,cr));
try{ t.sleep(5000); }catch(Exception ex){}
}
}
}
}
//









// to create own thread
import java.applet.*;
import java.awt.*;
public class Th7 extends Applet implements Runnable
{
Thread t;
int a=0,b=400;
Image m;
public void init()
{
m = getImage(getCodeBase(),"create.gif");
t = new Thread(this); // to create the new Thread for current applet
t.start();
}
public void run()
{
while(true)
{
a+=5;
b-=5;
if(a>=400)
{
a =0;
b = 400;
}
try{ t.sleep(100); }catch(Exception ex){}
repaint();
}
}
public void paint(Graphics g)
{
g.drawImage(m,a,a,this);
g.drawImage(m,b,b,this);
g.drawImage(m,b,a,this);
g.drawImage(m,a,b,this);
g.drawImage(m,200,a,this);
g.drawImage(m,200,b,this);
g.drawImage(m,b,200,this);
g.drawImage(m,a,200,this);
}
}
//








// to create own thread
import java.applet.*;
import java.awt.*;
public class Th8 extends Applet implements Runnable
{
Thread t;
int a=200,b=200;
Image m;
public void init()
{
m = getImage(getCodeBase(),"create.gif");
t = new Thread(this); // to create the new Thread for current applet
t.start();
}
public void run()
{
while(true)
{
a+=5;
b-=5;
if(a>=400)
{
a = 200;
b = 200;
}
try{ t.sleep(20); }catch(Exception ex){}
repaint();
}
}
public void paint(Graphics g)
{
g.drawImage(m,a,a,this);
g.drawImage(m,b,b,this);
g.drawImage(m,b,a,this);
g.drawImage(m,a,b,this);
g.drawImage(m,200,a,this);
g.drawImage(m,200,b,this);
g.drawImage(m,b,200,this);
g.drawImage(m,a,200,this);
}
}
//








// to create own thread
import java.applet.*;
import java.awt.*;
public class Th9 extends Applet implements Runnable
{
Thread t;
int x,y,s,cr,cg,cb,c=0;
Graphics g;
public void init()
{
g = getGraphics(); // g hold the current applet Graphics , now we can drawing without using paint method
t = new Thread(this); // to create the new Thread for current applet
t.start();
}
public void run()
{
while(true)
{
c++;
if(c>=200)
{
repaint();
c = 0;
}
drawing();
try{ t.sleep(20); }catch(Exception ex){}
}
}
void drawing()
{
x = (int)(Math.random()*800);
y = (int)(Math.random()*600);
s = (int)(Math.random()*50);
cr = (int)(Math.random()*255);
cg = (int)(Math.random()*255);
cb = (int)(Math.random()*255);
g.setColor(new Color(cr,cg,cb));
g.setFont(new Font("Courier New",3,s));
g.drawString("Gaurav",x,y);
}
}
//











// to create own thread
import java.applet.*;
import java.awt.*;
public class Th10 extends Applet implements Runnable
{
Thread t;
int x,y,w,h,cr,cg,cb,c=0;
Graphics g;
public void init()
{
g = getGraphics(); // g hold the current applet Graphics , now we can drawing without using paint method
t = new Thread(this); // to create the new Thread for current applet
t.start();
}
public void run()
{
while(true)
{
c++;
if(c>=300)
{
repaint();
c = 0;
}
drawing();
try{ t.sleep(10); }catch(Exception ex){}
}
}
void drawing()
{
x = (int)(Math.random()*800);
y = (int)(Math.random()*600);
w = (int)(Math.random()*50);
h = (int)(Math.random()*50);
cr = (int)(Math.random()*255);
cg = (int)(Math.random()*255);
cb = (int)(Math.random()*255);
g.setColor(new Color(cr,cg,cb));
g.fillOval(x,y,w,h);
}
}
//





// to create own thread
import java.applet.*;
import java.awt.*;
public class Th11 extends Applet implements Runnable
{
Thread t;
int a=0;
Image m[] = new Image[9];
public void init()
{
for(int i=0;i<8;i++)
m[i] = getImage(getCodeBase(),"aa"+i+".jpg");
t = new Thread(this); // to create the new Thread for current applet
t.start();
}
public void run()
{
while(true)
{
a++;
if(a>5)
a =0;
try{ t.sleep(1500); }catch(Exception ex){}
repaint();
}
}
public void paint(Graphics g)
{
g.drawImage(m[a],100,100,400,500,this);
}
}
//