By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

Thread Synchronization Not Working

sreekandank
P: 45
I have written the following code to demonstrate thread synchronization.
Expand|Select|Wrap|Line Numbers
  1. class ChildThread extends Thread
  2. {
  3.  String msg[]={"This","is","a","synchronized","variable"};
  4.  public void run()
  5.  {
  6.   display();
  7.  }
  8.  public synchronized void display()
  9.  {
  10.   for(int i=0;i<=4;i++)
  11.   {
  12.    System.out.println(msg[i]);
  13.    try
  14.    {
  15.     Thread.sleep(1000);
  16.    }
  17.    catch(InterruptedException e)
  18.    {
  19.     System.out.println(e);
  20.    }
  21.   }
  22.  }
  23. }
  24. class SynchronizedThread
  25. {
  26.  public static void main(String args[])
  27.  {
  28.   ChildThread ct1=new ChildThread();
  29.   ct1.start();
  30.   ChildThread ct2=new ChildThread();
  31.   ct2.start();
  32.  }
  33. }
  34.  
The above code produced the output that is not the one expected by me. So kindly help me anyone.Thanks in advance.
Oct 18 '12 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,421
It's hard to help, seeing as how we don't know what you're expecting.
Oct 18 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.