thread help

Discussion in 'Java' started by jammytech@gmail.com, May 29, 2006.

  1. Guest

    I'm a newbie to java and I have the following Q -

    class mythread implements Runnable {

    ..
    ..
    ..


    }



    scenario 1

    mythread my = new mythread();
    Thread t1 = new Thread(my);
    Thread t2 = new Thread(my);
    t1.start();
    t2.start();


    scenario 2

    mythread my1 = new mythread();
    mythread my2 = new mythread();
    Thread t1 = new Thread(my1);
    Thread t2 = new Thread(my2);
    t1.start();
    t2.start();


    Whats the difference b/w scenario 1 and 2 ?. I'm kind of confused here.
    Debugger shows me that two threads are created in both scenarios.

    -j
     
    , May 29, 2006
    #1
    1. Advertising

  2. VisionSet Guest

    <> wrote in message
    news:...
    > scenario 1
    >
    > mythread my = new mythread();
    > Thread t1 = new Thread(my);
    > Thread t2 = new Thread(my);
    > t1.start();
    > t2.start();
    >
    >
    > scenario 2
    >
    > mythread my1 = new mythread();
    > mythread my2 = new mythread();
    > Thread t1 = new Thread(my1);
    > Thread t2 = new Thread(my2);
    > t1.start();
    > t2.start();
    >
    >
    > Whats the difference b/w scenario 1 and 2 ?. I'm kind of confused here.
    > Debugger shows me that two threads are created in both scenarios.


    1/ You create a single instance and run it as 2 threads. So changes made to
    instance variables in one thread will be seen by the other thread.
    2/ You create two instances of the same class and run them as 2 separate
    threads. So instance variables are not shared, but static members of course
    still will be.

    --
    Mike W
     
    VisionSet, May 29, 2006
    #2
    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. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,566
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    517
  3. Stephen Miller
    Replies:
    3
    Views:
    3,994
    Stephen Miller
    Jul 2, 2004
  4. Johanna
    Replies:
    0
    Views:
    616
    Johanna
    Oct 13, 2004
  5. =?Utf-8?B?Sm9oYW5uYQ==?=
    Replies:
    3
    Views:
    1,972
    =?Utf-8?B?Sm9oYW5uYQ==?=
    Oct 15, 2004
Loading...

Share This Page