Query:multithread about java

Discussion in 'Java' started by Jack Dowson, May 3, 2007.

  1. Jack Dowson

    Jack Dowson Guest

    Hello Everybody:
    I'm new to java.
    Here is a demo about java's multithread character:

    class MultiThread extends Thread{
    private int ticket = 100;
    MultiThread(String name){
    super(name);
    }
    public void run(){
    while(ticket>0){
    System.out.println(ticket-- +" is saled by " +
    currentThread().getName());
    }
    }
    }
    public class MultiThreadDemo{
    public static void main(String[] args){
    MultiThread m1 = new MultiThread("Window 1");
    MultiThread m2 = new MultiThread("Window 2");
    MultiThread m3 = new MultiThread("Window 3");
    m1.start();
    m2.start();
    m3.start();
    }
    }

    When I run this program I had checked the process option card of task
    manager at the same time.I found there are 11 threads in the process
    java(sometimes maybe 4).

    I'm confused.
    Who can tell me what those threads indeed are if it's possible to identify.
    And another problem:what's the time interval for each timeslice?

    Any help will be greatly appreciated!
    Jack Dowson, May 3, 2007
    #1
    1. Advertising

  2. Jack Dowson

    Ramesh Guest

    On May 3, 7:09 pm, Jack Dowson <> wrote:
    > Hello Everybody:
    > I'm new to java.
    > Here is a demo about java's multithread character:
    >
    > class MultiThread extends Thread{
    > private int ticket = 100;
    > MultiThread(String name){
    > super(name);
    > }
    > public void run(){
    > while(ticket>0){
    > System.out.println(ticket-- +" is saled by " +
    > currentThread().getName());
    > }
    > }
    > }
    > public class MultiThreadDemo{
    > public static void main(String[] args){
    > MultiThread m1 = new MultiThread("Window 1");
    > MultiThread m2 = new MultiThread("Window 2");
    > MultiThread m3 = new MultiThread("Window 3");
    > m1.start();
    > m2.start();
    > m3.start();
    > }
    > }
    >
    > When I run this program I had checked the process option card of task
    > manager at the same time.I found there are 11 threads in the process
    > java(sometimes maybe 4).
    >
    > I'm confused.
    > Who can tell me what those threads indeed are if it's possible to identify.
    > And another problem:what's the time interval for each timeslice?
    >
    > Any help will be greatly appreciated!


    Dowson,
    When ever you run a java program a JVM is started
    which needs to start some threads for housekeeping purposes (like
    classloaders and garbage collectors). Those other threads you
    mentioned may have been created for such purposes. The number of such
    threads are usually system dependent. Also there will be more
    housekeeping threads for more complex programs such as servers.

    Thanks,
    Ramesh
    Ramesh, May 3, 2007
    #2
    1. Advertising

  3. Jack Dowson

    Jack Dowson Guest

    Thank you so much!Ramesh!
    Would you mind doing me a favour again?
    There are two Demos using different way to creat a thread,one is by
    inheriting class Thread while another is by implementing interface
    Runnable,but the results of these two examples are quite different.

    Demo1:
    class MultiThread4 implements Runnable{
    private int ticket = 100;
    public void run(){
    while(ticket>0)
    System.out.println(ticket-- +"is saled by " +
    Thread.currentThread().getName());
    }
    }
    class MultiThreadDemo4{
    public static void main(String[] name){
    MultiThread4 m =new MultiThread4();
    Thread t1 = new Thread(m,"Window 1");
    Thread t2 = new Thread(m,"Window 2");
    Thread t3 = new Thread(m,"Window 3");
    t1.start();
    t2.start();
    t3.start();
    }
    }

    Demo2:
    class NMultiThread4 extends Thread{
    NMultiThread4(String name){
    super(name);
    }
    private int ticket = 100;
    public void run(){
    while(ticket>0)
    System.out.println(ticket-- +"is saled by " +
    Thread.currentThread().getName());
    }
    }
    class NMultiThreadDemo4{
    public static void main(String[] name){
    NMultiThread4 t1 = new NMultiThread4("Window 1");
    NMultiThread4 t2 = new NMultiThread4("Window 1");
    NMultiThread4 t3 = new NMultiThread4("Window 1");
    t1.start();
    t2.start();
    t3.start();
    }
    }

    Why?
    Thanks in advance!
    Jack Dowson, May 3, 2007
    #3
  4. Jack Dowson

    Lew Guest

    Jack Dowson wrote:
    > There are two Demos using different way to creat a thread,one is by
    > inheriting class Thread while another is by implementing interface
    > Runnable,but the results of these two examples are quite different.
    >
    > Demo1:
    > class MultiThread4 implements Runnable{
    > private int ticket = 100;
    > public void run(){
    > while(ticket>0)
    > System.out.println(ticket-- +"is saled by " +
    > Thread.currentThread().getName());
    > }
    > }
    > class MultiThreadDemo4{
    > public static void main(String[] name){
    > MultiThread4 m =new MultiThread4();
    > Thread t1 = new Thread(m,"Window 1");
    > Thread t2 = new Thread(m,"Window 2");
    > Thread t3 = new Thread(m,"Window 3");
    > t1.start();
    > t2.start();
    > t3.start();
    > }
    > }


    Your Runnable example reuses the same Runnable object for all threads. Your
    Thread example did not do that.

    Try this instead:

    class MultiThreadDemo4
    {
    public static void main(String[] name)
    {
    Thread t1 = new Thread( new MultiThread4(), "Window 1" );
    Thread t2 = new Thread( new MultiThread4(), "Window 2" );
    Thread t3 = new Thread( new MultiThread4(), "Window 3" );
    t1.start();
    t2.start();
    t3.start();
    }
    }

    --
    Lew
    Lew, May 3, 2007
    #4
  5. Jack Dowson

    Jack Dowson Guest

    Thank you very much!Lew!
    I've tried and it's the same result as before modified!

    > Your Runnable example reuses the same Runnable object for all threads.
    > Your Thread example did not do that.

    Why?
    Jack Dowson, May 3, 2007
    #5
  6. Jack Dowson

    Ramesh Guest


    > > Your Runnable example reuses the same Runnable object for all threads.

    You have only a single Runnable object created and that objects
    method(run()) is called from three threads. And there is only a single
    shared variable 'ticket' of that same Runnable object for all the
    threads.

    > Why?

    So the three threads decrement the same 'ticket' variable and this
    runs 1/3 times faster(the 3 threads combined, needs to decrement the
    'ticket' only a hundred times) than the way where you create a new
    runnable object for each thread(each thread need to decrement its own
    Runnable objects 'ticket' variable 100 times). During the latter case
    each runnable object has its own 'ticket' variable and this is passed
    on to each of the thread and it is not shared.

    These are some of the rudimentary concepts in Java. If you are not
    familiar with these concepts I recommend you to get a copy of 'The
    Java Programming Language' by James Gosling et al.

    Thanks,
    Ramesh
    Ramesh, May 4, 2007
    #6
    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. =?Utf-8?B?RWtlbXBk?=

    Need an advice with this (multithread)

    =?Utf-8?B?RWtlbXBk?=, Feb 1, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    428
    Steve C. Orr [MVP, MCSD]
    Feb 1, 2005
  2. steve
    Replies:
    1
    Views:
    3,787
    Larry Barowski
    Jun 15, 2004
  3. Dimitri Ognibene
    Replies:
    1
    Views:
    323
    Oliver Wong
    May 1, 2006
  4. Michael Shestero

    stream in multiprocess (multithread) apps

    Michael Shestero, Dec 19, 2003, in forum: C++
    Replies:
    4
    Views:
    522
    Michael Shestero
    Dec 19, 2003
  5. r_obert@REMOVE_THIS.hotmail.com

    Multithread or Multithread DLL?

    r_obert@REMOVE_THIS.hotmail.com, Nov 27, 2004, in forum: C++
    Replies:
    0
    Views:
    2,222
    r_obert@REMOVE_THIS.hotmail.com
    Nov 27, 2004
Loading...

Share This Page