Java Threads Without Locks Example Program

By | December 26, 2010
You can find here Threads without locks example program.Here you can find the thread program based on without synchronized block menthod.
class TwoStrings {
static void print (String str1, String str2) {
System.out.print(str1);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
e.printStackTrace();
}
System.out.println(str2);
}
}
class PrintStringsThread implements Runnable {
String str1, str2;
PrintStringsThread(String str1, String str2) {
this.str1 = str1;    this.str2 = str2;
new Thread(this).start();
}
public void run() {
TwoStrings.print(str1, str2);
}
}
public class ThreadUnsyncDemo {
public static void main(String args[]) {
new PrintStringsThread (“Hello “, “there.”);
new PrintStringsThread (“How are “, “you?”);
new PrintStringsThread (“Thank you “,”very much!”);
}
}

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

Leave a Reply

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