473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Thread Synchronization Not Working

sreekandank
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
1 1689
Rabbit
12,516 Expert Mod 8TB
It's hard to help, seeing as how we don't know what you're expecting.
Oct 18 '12 #2

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

Similar topics

7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
0
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
10
by: Teis Draiby | last post by:
In an application manipulating streaming video and 3D stuff I want to implement mutithreading to ensure a decent UI response time. Since my application is very speed critical I want to use the...
1
by: Christoph Nahr | last post by:
The task: I want to let a background thread and a Windows Forms foreground thread communicate via callback methods. Now synchronization of a *single* delegate works just fine with the usual...
6
by: Daniel | last post by:
i have an array that i want all threads to be able to READ from concurrently, however, at times i want to UPDATE the array. at which point i want all threads that use it to block when they try to...
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
2
by: Don Tucker | last post by:
Hello, I am using Visual Studio 2005 .Net, coding in C#. I am working through the threading walkthrough: ...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
2
by: Maqsood Ahmed | last post by:
Hello, I asked a question regarding thread synchronization on this usergroup some days back. I was using the AutoResetEvent class for signalling between two threads. The problem with...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.