Threads question

Discussion in 'Java' started by fishfry, Jun 29, 2004.

  1. fishfry

    fishfry Guest

    I'm learning about threads and I noticed there are two different ways I
    can implement a runnable object. In one way, I have one object and two
    threads; in the other way, I have two objects and two threads. Like this:


    // Two objects, two threads
    public class Counter implements Runnable {
    Thread t;
    int Count;

    Counter(String threadname) {
    Count=0;
    t = new Thread(this, threadname);
    t.start();
    }

    public void run() {
    while(t == Thread.currentThread()) {
    Count++;
    System.out.println("Thread " + t.getName()
    + " count = " + Count);
    try {
    t.sleep(1000); // in milliseconds
    } catch (InterruptedException e) {}
    }
    }

    public void stop() {
    t = null;
    }

    public static void main(String args[]) {
    // Two objects.
    Counter c1 = new Counter("foo");
    Counter c2 = new Counter("bar");
    }
    }

    The output of this program is

    Thread foo count = 1
    Thread bar count = 1
    Thread foo count = 2
    Thread bar count = 2
    Thread foo count = 3
    Thread bar count = 3
    Thread foo count = 4
    Thread bar count = 4
    etc.

    Since each thread operates on its own instance of the object Counter,
    each thread has its own copy of the instance variable Count.



    // One object, two threads.
    public class Counter2 implements Runnable {
    int Count = 0;

    Counter2() {
    }

    public void run() {
    Thread t;
    t = Thread.currentThread();
    while(true) {
    Count++;
    System.out.println("Thread " + t.getName()
    + " count = " + Count);
    try {
    t.sleep(1000); // in milliseconds
    } catch (InterruptedException e) {}
    }
    }

    public void stop() {
    // t = null;
    }

    public static void main(String args[]) {
    Counter2 c1 = new Counter2();

    // One object, two threads.
    Thread t1 = new Thread(c1);
    Thread t2 = new Thread(c1);

    t1.start();
    t2.start();
    }
    }

    The output is

    Thread Thread-0 count = 1
    Thread Thread-1 count = 2
    Thread Thread-0 count = 3
    Thread Thread-1 count = 4
    Thread Thread-0 count = 5
    Thread Thread-1 count = 6
    Thread Thread-0 count = 7
    Thread Thread-1 count = 8
    etc.

    because there are two threads, each sharing the same object.

    Are there names for these two ways of doing things? What are the
    implications for synchronization, etc.? What else should I know about
    these? And why don't any of the tutorials mention this interesting
    distinction?
    fishfry, Jun 29, 2004
    #1
    1. Advertising

  2. fishfry

    Roedy Green Guest

    On Tue, 29 Jun 2004 04:48:09 GMT, fishfry
    <> wrote or quoted :

    >because there are two threads, each sharing the same object.
    >
    >Are there names for these two ways of doing things?


    Correct and incorrect.

    The second way, if you run it long enough, you will eventually get
    strange anomalies. You have to make sure the ++ is done atomically by
    synchronising, not interleaving the fetch, add, store from two
    different threads.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 29, 2004
    #2
    1. Advertising

  3. fishfry

    Chris Smith Guest

    fishfry wrote:
    > Are there names for these two ways of doing things? What are the
    > implications for synchronization, etc.? What else should I know about
    > these? And why don't any of the tutorials mention this interesting
    > distinction?


    You typically don't want to do the second of the two. It means that
    instance fields of the Runnable are shared state, and need to be
    protected with synchronization to guarantee consistent state. Something
    that close the the thread's immediate task almost certainly ought to be
    non-shared.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 29, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Marden
    Replies:
    0
    Views:
    1,067
    Nick Marden
    Jan 17, 2004
  2. yoda
    Replies:
    2
    Views:
    426
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    391
    William Ahern
    Aug 27, 2004
  4. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,419
    Arne Vajhøj
    Apr 9, 2008
  5. Une bévue
    Replies:
    0
    Views:
    138
    Une bévue
    Jun 14, 2006
Loading...

Share This Page