Threads - check if runs correctly

Hello Experts,

I have four classes below [in the code snippet] one Updater, one Checker, one Data, and one ThreadTest which runs the two threads.

what i want is to create three different variations of checking that the code always runs correctly
1)one  which uses synchronized methods
 2)one which uses Java 5 locks
 3)one which uses semaphores

Can anyone tell me how can i do the above checks ?

Thanks in advance!
public class ThreadTest {
	public static void main( )
	      //declare the threads
	      ThreadTest Updater = new ThreadTest( );
	      ThreadTest Checker = new ThreadTest( );
	      System.err.println( "Started two Threads, main ends\n" );
	private void start() {
public class Data { 
 Integer counter1 = 0; 
 Integer counter2 = 0; 
 Integer counter3 = 0; 
 Integer add() {   
     Integer temp1 = counter1; 
     Integer temp2 = counter2; 
     Integer temp3 = counter3; 
     return temp1 + temp2 + temp3; 
 void inc () { 
      counter1 = counter1 + 1; 
      counter2 = counter2 + 1; 
      counter3 = counter3 + 1; 
public class Updater implements Runnable { 
      Data data = null; 
    Updater(Data data) { = data; 
    public void run () { 
          for(Integer i = 0; i < 50000; i++)  
         System.out.println("Updater ended normally"); 
public class Checker implements Runnable { 
     Data data = null; 
     Checker(Data data) { = data; 
     public void run () { 
         for(Integer i = 0; i < 50000; i++) { 
           Integer r = data.add(); 
           if (!(r % 3 == 0)) { 
              // As inc() in Data increments all three counters, 
              // the result of read() should always be a multiple 
              // of 3, so this should never happen.... 
              System.out.println("Checker did NOT end normally!");       
         System.out.println("Checker ended normally"); 

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
was wondering what answer should quality for grade A? ready made code?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.