Thursday 11 August 2016

Threads program in java

class y
{
  synchronized void doWork(String string)
{
        System.out.println("Starting " + string);
        try {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {}
        System.out.println("Ending " + string);
}
}

class X extends Thread
{
y p;
String string2;
public X(y string1,String string2)
{
super(string2);
p=string1;
this.string2=string2;
}
public void run()
{
p.doWork(Thread.currentThread().getName());
}
}

class process
{
public static void main(String args[])
{
y p=new y();
X t1=new X(p,"one");
X t2=new X(p,"two");
X t3 =new X(p,"three");
X t4=new X(p,"four");
X t5=new X(p,"five");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}

No comments: