0% found this document useful (0 votes)
7 views8 pages

Java Thread Class Methods Explained

Uploaded by

Satya Narayana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Java Thread Class Methods Explained

Uploaded by

Satya Narayana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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();
[Link](name+"begins its execution");
for (int i=1;i<=20;i++){
[Link](name+":"+i);
if(i==10){
[Link](name+"is suspended");
suspend();
[Link](name()+"resumes its execution:");
}
}
[Link](name+"Ends:");
}
public static void main(String[] args) {

Thread1 t1=new Thread1();


Thread1 t2=new Thread1();
[Link]("Thread-1");
[Link]("Thread-2");
[Link]();
[Link]();
try{[Link](2000);}catch(Exception e){}
[Link]();
[Link]();
}
}
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();
[Link](name+"begins its execution");
for (int i=1;i<=20;i++){
[Link](name+":"+i);
if([Link]("Thread-1")){
[Link](name+"is stopped its execution-Dead State");
stop();
}
}
[Link](name+"Ends:");
}
public static void main(String[] args) {

Thread2 t1=new Thread2();


Thread2 t2=new Thread2();
[Link]("Thread-1");
[Link]("Thread-2");
[Link]();
[Link]();

}
}
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();
[Link](name+"begins its execution");
for (int i=1;i<=10;i++){
[Link](name+":"+i);
yield();
}
[Link](name+"Ends:");
}
public static void main(String[] args) {

Thread3 t1=new Thread3();


Thread3 t2=new Thread3();
[Link]("Thread-1");
[Link]("Thread-2");
[Link]();
[Link]();
}
}
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, [Link]();
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();
[Link](name+"begins its execution");
for (int i=1;i<=20;i++){
[Link](name+":"+i);
}
[Link](name+"Ends:");
}
public static void main(String[] args)throws InterruptedException {

Thread4 t1=new Thread4();


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

[Link]("IS Child Thread Daemon:"+[Link]());


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

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

[Link]("WITHDRAW OPERATION IS PERFOMRING");


if(balance<amount)
{
[Link]("NO SUFFICIENT BALANCE TO WITH DRAW WAITING For
deposit");
try{[Link](3000); wait();}
catch(Exception e){}
}

balance-=amount;
[Link]("WITHDRAW MONEY SUCCESSFULLY:");

}
public synchronized void deposit(double amount){

[Link]("DEPOSIT OPERATION IS PERFOMRING");


try{[Link](3000); }catch(Exception e){}
balance+=amount;
[Link]("DEPOSIT OPERATION COMPLETED SUCCESSFULLY:");
notify();

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

public void run()


{
[Link](7000);
}
}
class inter
{

public static void main(String[] args)


{
Customer c=new Customer();
Thread1 t1=new Thread1(c);
Thread2 t2=new Thread2(c);
[Link]();
[Link]();
}
}

Common questions

Powered by AI

Inter-thread communication in Java is achieved through 'wait()', 'notify()', and 'notifyAll()' methods. 'wait()' puts the current thread in a waiting state and is only called within a synchronized block. 'notify()' wakes up a single waiting thread among those that called 'wait()' on the same object, while 'notifyAll()' wakes all threads that are waiting. This mechanism is crucial for coordinating actions among multiple threads, allowing a thread to pause execution until a specific condition is met by another thread .

Using 'suspend()' without ensuring the correct release of system resources and locks can lead to significant issues such as deadlocks, because a suspended thread retains all of its locks. This prevents other threads from acquiring those locks, blocking any dependent operations. Furthermore, resources that require a particular order of execution or transaction could remain locked indefinitely, leading to system instability and decreased performance .

The 'yield()' method, when invoked, suggests to the thread scheduler that the current thread is willing to relinquish its current use of the CPU. This allows other threads of the same priority to have a chance to execute if they are in a runnable state. However, there is no guarantee that a yielding thread will actually yield control or that it will not immediately be scheduled again; it depends on the thread scheduler's implementation and the thread priorities. This method does not relinquish any locks the thread holds .

The 'stop()' method is used to terminate a thread's execution. Once a thread is stopped, it goes into a dead state and cannot be restarted, which may leave resources in an inconsistent state. This method is discouraged because it forces the thread to terminate immediately without ensuring that the thread has released resources or locks, potentially leading to deadlocks and resource leaks .

Daemon threads in Java are specialized threads that provide services to user threads in the background. These threads run low-priority tasks, such as garbage collection, and automatically terminate when all user threads finish execution. Use cases for Daemon threads include tasks that support the general execution environment of the application or carry out housekeeping activities, such as clearing logging files, waiting for socket connections, or monitoring operating system processes .

Synchronized methods in Java ensure that only one thread executes at a time, preventing data inconsistency and ensuring thread-safe operations by using locks. This is vital in scenarios where shared resources are accessed concurrently by multiple threads. However, excessive use of synchronized methods can lead to performance degradation, as threads are forced to wait longer to acquire the locks, potentially causing contention and reducing throughput in highly parallel environments .

The 'suspend()' method is used to temporarily halt a thread's execution, putting it in a waiting state, while the 'resume()' method restarts a suspended thread. This functionality is useful when a thread should pause for an event before continuing execution. However, using 'suspend()' and 'resume()' is risky because if a suspended thread holds a lock, it can cause a deadlock when another thread attempts to acquire the same lock, but can't because the suspended thread hasn't released it .

Thread priorities in Java determine the preference of execution among runnable threads. The thread scheduler uses these priorities as a hint to decide which thread to run next, although it is not bound to respect them. Threads with higher priority are more likely to execute before those with lower priority. However, priority does not guarantee a strict implementation because it depends on the underlying processor and operating system .

'join()' affects thread execution order by pausing the calling thread until the specified thread completes. For example, in a program where multiple threads perform different tasks that must finish in sequence, 'join()' ensures that subsequent operations only commence once specific threads have concluded their execution. This mechanism is vital for dependent processes where consistency and order of task completion are crucial, such as in data loading and preprocessing before analysis .

The 'join()' method is used to cause the current thread to wait for another thread to complete its execution. This can be useful in managing thread execution, as it ensures that the thread calling 'join()' will not proceed until the specified thread has finished. This is crucial for scenarios where subsequent operations depend on the completion of certain tasks executed by other threads, ensuring order and consistency in thread execution .

You might also like