Threads Synchronization in Java with Example

By | December 26, 2010
You can find here Threads Synchronization example program.This threads synchronization is based on lock Synchronize concept.

You can find here Threads Synchronization example program.This threads synchronization is based on lock Synchronize concept.

class TwoStrings1 {

synchronized static void print (String str1, String str2){
System.out.print(str1);
try{
Thread.sleep(1000);
} catch (InterruptedException ie) {
e.printStackTrace();
}
System.out.println(str2);
}
}
class PrintStringsThread1 implements Runnable {
String str1, str2;
PrintStringsThread1(String str1, String str2) {
this.str1 = str1;
this.str2 = str2;
new Thread(this).start();
}
public void run() {
TwoStrings1.print(str1, str2);
}
}
public class ThreadSyncDemo {
public static void main(String args[]) {
new PrintStringsThread1 (“Hello “, “there.”);
new PrintStringsThread1 (“How are “, “you?”);
new PrintStringsThread1 (“Thank you “,”very much!”);
}
}

Result:

Hello there.
How are you?
Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *