Professional Documents
Culture Documents
Multi Threading
Multi Threading
class MyThreadDemo
t.start();
………………………………………………………………………………………………………………………………………………….
class MyThreadDemo
{
mt.start();
……………………………………………………………………………………………………………………
System.out.println("r1 ");
try {
Thread.sleep(500);
catch(InterruptedException ie)
// do something
System.out.println("r2 ");
t1.start();
t2.start();
System.out.println(t1.isAlive());
System.out.println(t2.isAlive());
}
……………………………………………………………………………………………………………
System.out.println("r1 ");
try {
Thread.sleep(500);
}catch(InterruptedException ie){ }
System.out.println("r2 ");
t1.start();
try{
}catch(InterruptedException ie){}
t2.start();
…………………………………………………………………………….
class First
{
public void display(String msg)
System.out.print ("["+msg);
try
Thread.sleep(1000);
catch(InterruptedException e)
e.printStackTrace();
System.out.println ("]");
String msg;
First fobj;
fobj = fp;
msg = str;
start();
fobj.display(msg);
}
}
…………………………………………………………………………………………
class Customer
int amount=10000;
System.out.println("going to withdraw...");
if(this.amount<amount)
try{wait();
catch(Exception e)
}
this.amount-=amount;
System.out.println("withdraw completed...");
System.out.println("going to deposit...");
this.amount+=amount;
notify();
class Test
new Thread()
.start();
new Thread()
c.deposit(10000);
}
.start();
……………………………………………………………………………………………………….
…………………………………………………………………………………………………….
…………………………………………………………………………………………………………….