Results 1 to 4 of 4
  1. #1
    bhanu is offline Member
    Join Date
    Oct 2007
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Question not much wrong ....

    Quote Originally Posted by bhanu View Post
    ...,I have been trying for a long time to find out multiple threads access the shared data.
    How, why?
    Quote Originally Posted by bhanu View Post
    I have written a sample code, there I my intention is that method has to be accessed onlny one thread at a time. In other words. only one thread finish it's 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.
    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

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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...

  4. #4
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

Similar Threads

  1. FileLock and file access from concurrent threads..
    By fxRichard in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2009, 09:08 PM
  2. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 07:20 PM
  3. Replies: 0
    Last Post: 07-13-2008, 02:47 PM
  4. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:25 AM
  5. Replies: 1
    Last Post: 08-07-2007, 08:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •