Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Query:multithread about java

Thread view: 
Jack Dowson - 03 May 2007 15:09 GMT
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!
Ramesh - 03 May 2007 15:49 GMT
> Hello Everybody:
> I'm new to java.
[quoted text clipped - 32 lines]
>
> 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
Jack Dowson - 03 May 2007 16:16 GMT
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!
Lew - 03 May 2007 16:37 GMT
> There are two Demos using different way to creat a thread,one is by
> inheriting class Thread while another is by implementing interface
[quoted text clipped - 20 lines]
>         }
>     }

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();
  }
 }

Signature

Lew

Jack Dowson - 03 May 2007 17:12 GMT
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?
Ramesh - 04 May 2007 07:53 GMT
> > 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.