Unit 5 - Java Multithreading
Unit 5 - Java Multithreading
/
Java Programming
By Melese E.
Multi-threading Concept
class Main{
public static void main(String args[]){
System.out.println("in the main method");
Threading2 th = new Threading2();
System.out.println(th.t.getName());
th.t.start();
System.out.println(Thread.currentThread().getName());
System.out.println("This is also in the main thread");
}
}
}}} Tuesday, May 21, 2024 By Melese E., Department of Computer Science 19
Java Multi-threading
• Synchronization – using a synchronized method
• Modifying the display method in the Example class by modifying it with the
synchronized keyword prevents threads to enter the method at the same
time
synchronized void display(String msg, String threadName) throws InterruptedException{
System.out.println(threadName + " displays " + msg);
Thread.sleep(1000);
System.out.println(threadName + " displays " + msg + " again");
}