I am confused as to what will be the output of the following code(between the two options) and why: As per my understanding output should be option1 as synchronized keyword is used .
Output Options:
- Java Thread ThreadExample JavaMultithreading.
- The order in which text will be printed cannot be determined.
class ThreadDemo extends Thread
{
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
public static void main(String args[])
{
final ThreadDemo h = new ThreadDemo();
new Thread()
{
public void run()
{
synchronized(this)
{
h.sb1.append("Java");
h.sb2.append("Thread");
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(this)
{
h.sb1.append("Mutithreading");
h.sb2.append("Example");
System.out.println(h.sb2);
System.out.println(h.sb1);
}
}
}.start(); }}