MultiThreaded Programming  «Prev  Next»

Lesson 7Thread synchronization
ObjectiveExamine thread synchronization and how it impacts multithreaded programming.

How Thread Synchronization impacts Multithreaded Programming

Consider the ramifications of multiple threads sharing a common piece of data. One thread might interrupt another thread while it is in the process of modifying the data. Since threads execute concurrently, there is no way to know when each thread will access the data.
Synchronization refers to the process of structuring threads so that they never interrupt each other while performing sensitive tasks. You can designate a method or a section of code as being synchronized, which means that a thread cannot be interrupted until it is finished executing all of the synchronized code. You designate a method or a section of code as being synchronized by using the synchronized keyword:

public synchronized calculate() {
  // This code is synchronized
}

Synchronization has a high cost, which is inefficiency. One synchronized method call can take more than four times as long to execute as a nonsynchronized method call. Therefore, indescriminate use of synchronized keyword is not a good idea. It is better to analyze and deal with synchronization issues of multithreaded programs on an individual basis.

What would be the result of an attempt to compile and run the following code?
class SimpleThread extends Thread { 
 public SimpleThread(String str){ super(str);  }
 public void run(){ 
  for (int i = 0; i < 10; i++){
   System.out.println(i + " " + getName());
   try{ 
    sleep((long)(Math.random() * 1000));
   } 
   catch (InterruptedException e) {} 
  } // end - for
  System.out.println("DONE! " + getName());
 }
}
public class TwoThreadsDemo{ 
 public static void main (String[] args) {
  new SimpleThread("Java Programmer").start(); // line A 
  new SimpleThread("Java Programmer").start(); // line B
 }
}

Please select 1 option:
  1. It will not compile because SimpleThread is a subclass of thread, but does not have a no-argument public constructor.
  2. It will not compile because two separate threads in line A and line B have been given the same name "Java Programmer".
  3. It will compile, but at runtime the second thread will overwrite the first thread and so the run method of the SimpleThread class will be executed only once instead of twice.
  4. It will compile but will throw RuntimeException.
  5. It will compile and run with success and the run method of SimpleThread class will be executed twice by two separate threads created.

Answer: e
Explanation:
  1. The thread name does not need to be unique, so two threads with the same name can execute separately.
  2. Any String can be used for the name of a thread. Here two threads are started and executed accordingly.
  3. There are no compiler errors in the given code, so choices B and C are incorrect.
  4. Choice D is incorrect because no exceptions are thrown by this code. Choice A is incorrect because there is no requirement for a no-args constructor in a Thread subclass.
  5. The correct answer is: It will compile and run with success and the run method of the SimpleThread class will be executed twice by two separate threads created.

For point 5, you have to know that there are 2 separate threads because the output will not tell you this the way the code is currently written. Note: In order to answer this question you have to change the code to the following.