A)
->whenever we want to execute serial processing then don’t write code in run(). Whenever we want for the parallel processing we write in run().
->login of application when dump in run() it is different, later when we execute serial processing so, again we redesigning the code.
//MyThreadApplication2.java
class printingNumber
{
void printOnetoThirty()
{
for (int i=1;i<=30;i++)
System.out.println(i);
}
voidprintseventytohundred()
{
for(int i=70;i<=100;i++)
{
System.out.println(i);
}
}
}
class Mythread2 extends Thread
{
printingNumber pn;
Mythread2(printingNumber p)
{
pn=p;
}
public void run()
{
if(getName().equals("one"))
pn.printOnetoThirty();
if(getName().equals("three"))
pn.printseventytohundred();
}
}class MyThreadApplication2 {
public static void main(String[] args) {
printingNumber p=new printingNumber();
Mythread2 t1= new Mythread2(p);
Mythread2 t2 = new Mythread2(p);
t1.setName("one");
t2.setName("three");
t1.start();
t2.start();
}
}
Comments
Post a Comment