[go: up one dir, main page]

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

Multi Threading

The document provides an overview of multi-threading in Java, covering thread methods, synchronization, inter-thread communication, and deadlock. It details various thread methods such as start, run, and join, along with synchronization syntax and an example implementation. Additionally, it explains inter-thread communication methods and the concept of deadlock in multi-threaded applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Multi Threading

The document provides an overview of multi-threading in Java, covering thread methods, synchronization, inter-thread communication, and deadlock. It details various thread methods such as start, run, and join, along with synchronization syntax and an example implementation. Additionally, it explains inter-thread communication methods and the concept of deadlock in multi-threaded applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Multi threading

● Thread methods
● Synchronization
● Inter thread communication
● Deadlock
Thread Methods

public void start()

public void run()

public final void setName(String name)

public final void setPriority(int priority)

public final void setDaemon(boolean on)

public final void join(long millisec)

public void interrupt()

public final boolean isAlive()


Static methods

public static void yield()

public static void sleep(long millisec)

public static boolean holdsLock(Object x)

public static Thread currentThread()

public static void dumpStack()


Synchronization

Syntax
synchronized(objectidentifier) {

// Access shared variables and other shared resources

}
Example
class ThreadDemo extends Thread {
private Thread t;
private String threadName;
class PrintDemo { PrintDemo PD;
public void printCount() {
try { ThreadDemo( String name, PrintDemo pd) {
for(int i = 5; i > 0; i--) { threadName = name;
System.out.println("Counter --- " + i ); PD = pd;
}
}
} catch (Exception e) { public void run() {
System.out.println("Thread interrupted."); synchronized(PD){
} PD.printCount();
} }
} System.out.println("Thread " + threadName + " exiting.");
}

public void start () {


System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}
Main method class
public class TestThread {
public static void main(String args[]) {

PrintDemo PD = new PrintDemo();

ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD );


ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD );

T1.start();
T2.start();

// wait for threads to end


try {
T1.join();
T2.join();
} catch ( Exception e) {
System.out.println("Interrupted");
}
}
Inter thread communication

Interthread communication is important when you develop an application where two or more threads exchange
some information.

Methods:

public void wait()

public void notify()

public void notifyAll()


Deadlock

● Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
● Deadlock occurs when multiple threads need the same locks but obtain them in different order.
● A Java multithreaded program may suffer from the deadlock condition because the synchronized
keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the
specified object.

You might also like