Results 1 to 4 of 4
- 01-13-2009, 12:58 PM #1
Member
- Join Date
- Oct 2007
- Posts
- 7
- Rep Power
- 0
multiple threads access the same method.
Hello,
I have been trying for a long time to find out multiple threads access the shared data.
I have written a sample code, there I my intention is that method has to be accessed
onlny one thread at a time., mean one thread finished the job, then next thread can
access the shared source. But for this code I am not getting the desired out put what I want. But if I am using synchronized block I am getting the output. Please correct where I got mistake. Please see my code.
public class TestThread implements Runnable {
Shared r;
public TestThread() {
r = new Shared();
}
public static void main(String args[]) {
Thread t1 = new Thread(new TestThread());
Thread t2 = new Thread(new TestThread());
t1.setName("A");
t2.setName("B");
t1.start();
t2.start();
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
r.count();
}
}
class Shared {
public synchronized void count() {
String name = Thread.currentThread().getName();
System.out.println(name + ":accessed...");
try {
for (int i = 0; i < 5; i++) {
System.out.println(name + ": " + i);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
Thanks
Bhanu lakshmi.
- 01-13-2009, 05:40 PM #2
not much wrong ....
How, why?
What are you getting?...post directly in code tags.
I started re-writing you code, but I do not see a much that is incorrect. It is a standard intro to threading issues, two threads, compete for same resouce. synchronized method prints, what's wrong with it? If anything, the code runs faster than the system can keep up with it so pretty much whatever it prints is at most a study practice.
What is the code doing or not doing that you intend for it to do or not do?Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 01-13-2009, 06:09 PM #3
Your test seems like it will run, but what are you expecting it to do?
It works whether Count() is synchronized or not.
Count() need not to be synchronized, since the two threads are working on separate instances TestThread, and each TestThread instance has its own Shared instance.
This is a good design in general, since you eliminate conflicts between the threads. However, the threads can't share any data.
I suspect your test example is simpler than you actual application...
- 02-16-2009, 06:54 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 68
- Rep Power
- 0
You may also want to look at similar scenarios here Multi Threading Scenarios
____________________________________________
Priya,
Cooking is Fun| Eat Healthy Stay Fit | Sweets | Raita | Bread | Dal| SnacksCheers,
Eat Healthy Stay Fit
Similar Threads
-
FileLock and file access from concurrent threads..
By fxRichard in forum Advanced JavaReplies: 5Last Post: 01-02-2009, 08:08 PM -
Method access or field access
By carderne in forum New To JavaReplies: 2Last Post: 12-06-2008, 06:20 PM -
How to store and access multiple calendars using a J2ME application
By thirupathik in forum CLDC and MIDPReplies: 0Last Post: 07-13-2008, 01:47 PM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:25 AM -
Help with access a database using Microsoft Access
By cachi in forum JDBCReplies: 1Last Post: 08-07-2007, 07:51 AM


LinkBack URL
About LinkBacks


Bookmarks