0% found this document useful (0 votes)
11 views

Thread Class Methods Programs

Uploaded by

Satya Narayana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Thread Class Methods Programs

Uploaded by

Satya Narayana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

suspend() method

The suspend() method of thread class puts the thread from running to
waiting state. This method is used if you want to stop the thread execution
and start it again when a certain event occurs. This method allows a thread
to temporarily cease execution. The suspended thread can be resumed using
the resume() method.
Syntax:-public final void suspend()
resume() method
The resume() method of thread class is only used with suspend() method.
This method is used to resume a thread which was suspended using
suspend() method. This method allows the suspended thread to start again.
Syntax:-public final void resume()
//Example on suspend() and resume() methods
class Thread1 extends Thread{

public void run(){


String name=getName();
System.out.println(name+"begins its execution");
for (int i=1;i<=20;i++){
System.out.println(name+":"+i);
if(i==10){
System.out.println(name+"is suspended");
suspend();
System.out.println(name()+"resumes its execution:");
}
}
System.out.println(name+"Ends:");
}
public static void main(String[] args) {

Thread1 t1=new Thread1();


Thread1 t2=new Thread1();
t1.setName("Thread-1");
t2.setName("Thread-2");
t1.start();
t2.start();
try{Thread.sleep(2000);}catch(Exception e){}
t1.resume();
t2.resume();
}
}
Java Thread stop() method
The stop() method of thread class terminates the thread execution. Once a thread is
stopped, it cannot be restarted by start() method.
//Example on stop() method.
class Thread2 extends Thread{

public void run(){

String name=getName();
System.out.println(name+"begins its execution");
for (int i=1;i<=20;i++){
System.out.println(name+":"+i);
if(name.equals("Thread-1")){
System.out.println(name+"is stopped its execution-Dead State");
stop();
}
}
System.out.println(name+"Ends:");
}
public static void main(String[] args) {

Thread2 t1=new Thread2();


Thread2 t2=new Thread2();
t1.setName("Thread-1");
t2.setName("Thread-2");
t1.start();
t2.start();

}
}
Yield() method :-
A yield() method is a static method of Thread class and it can stop the
currently executing thread and will give a chance to other waiting threads
of the same priority. If in case there are no waiting threads or if all the
waiting threads have low priority then the same thread will continue its
execution.

Syntax :- public static void yield()


//Example on yield() method.
class Thread3 extends Thread{

public void run(){


String name=getName();
System.out.println(name+"begins its execution");
for (int i=1;i<=10;i++){
System.out.println(name+":"+i);
yield();
}
System.out.println(name+"Ends:");
}
public static void main(String[] args) {

Thread3 t1=new Thread3();


Thread3 t2=new Thread3();
t1.setName("Thread-1");
t2.setName("Thread-2");
t1.start();
t2.start();
}
}
Join() method:-
The join method allows one thread to wait for the completion of another.
If t is a Thread object whose thread is currently executing, t.join();
causes the current thread to pause execution until t's thread terminates
//Example on yield() method.
class Thread4 extends Thread{

public void run(){


String name=getName();
System.out.println(name+"begins its execution");
for (int i=1;i<=20;i++){
System.out.println(name+":"+i);
}
System.out.println(name+"Ends:");
}
public static void main(String[] args)throws InterruptedException {

Thread4 t1=new Thread4();


t1.setName("Thread-1");
t1.start();
System.out.println("Main Thread Begins:");
for (int i=1;i<=20;i++){
System.out.println("Main-Thread-"+i);
if(i==1){
System.out.println("*******Main-Thread goes to waiting state*******");
t1.join();
}
}
}
//Example on Daemon Thread.
class MyThread extends Thread
{
public void run()
{
System.out.println("Child -Thread Running:");
for(int i=1;i<=20;i++)
System.out.println("child thread:"+i);
System.out.println("child thread ends:");
}
public static void main(String[] args)
{
MyThread m=new MyThread();
System.out.println("IS Main Thread
Daemon:"+Thread.currentThread().isDaemon());

System.out.println("IS Child Thread Daemon:"+m.isDaemon());


m.setDaemon(true);
m.start();
System.out.println("Main Thread Ends:");
}
}
//Thread Priorities.
class MyThread1 extends Thread
{
public void run()
{
System.out.println("child Thread Running with Prioirty:"+getPriority());
}
public static void main(String[] args)
{
MyThread1 m=new MyThread1();
m.start();
System.out.println("Main Running with
priority"+Thread.currentThread().getPriority());

}
}
class Thr1 extends Thread
{
public void run()
{
System.out.println("Child Thread begins:");
for(int i=1;i<=20;i++)
System.out.println("Child Thread:"+i);
System.out.println("Child Thread Ends:");
}
public static void main(String[] args)
{
Thr1 t1=new Thr1();
t1.setPriority(10);
t1.start();
System.out.println("Main Thread begins:");
for(int i=1;i<=20;i++)
System.out.println("Main:"+i);
System.out.println("Main1 Thread Ends:");
}
}
//Example on Inter-Thread Communication.
class Customer
{
double balance=10000;
public synchronized void withdraw(double amount)
{

System.out.println("WITHDRAW OPERATION IS PERFOMRING");


if(balance<amount)
{
System.out.println("NO SUFFICIENT BALANCE TO WITH DRAW WAITING For
deposit");
try{Thread.sleep(3000); wait();}
catch(Exception e){}
}

balance-=amount;
System.out.println("WITHDRAW MONEY SUCCESSFULLY:");

}
public synchronized void deposit(double amount){

System.out.println("DEPOSIT OPERATION IS PERFOMRING");


try{Thread.sleep(3000); }catch(Exception e){}
balance+=amount;
System.out.println("DEPOSIT OPERATION COMPLETED SUCCESSFULLY:");
notify();

}
}
class Thread1 extends Thread
{
Customer c;
public Thread1(Customer c){this.c=c;}
public void run()
{
c.withdraw(12000);
}
}
class Thread2 extends Thread
{
Customer c;
public Thread2(Customer c){this.c=c;}

public void run()


{
c.deposit(7000);
}
}
class inter
{

public static void main(String[] args)


{
Customer c=new Customer();
Thread1 t1=new Thread1(c);
Thread2 t2=new Thread2(c);
t1.start();
t2.start();
}
}

You might also like