Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Query:multithread about java

Reply
Thread Tools

Query:multithread about java

 
 
Jack Dowson
Guest
Posts: n/a
 
      05-03-2007
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!
 
Reply With Quote
 
 
 
 
Ramesh
Guest
Posts: n/a
 
      05-03-2007
On May 3, 7:09 pm, Jack Dowson <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Jack Dowson
Guest
Posts: n/a
 
      05-03-2007
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!


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-03-2007
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
 
Reply With Quote
 
Jack Dowson
Guest
Posts: n/a
 
      05-03-2007
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?
 
Reply With Quote
 
Ramesh
Guest
Posts: n/a
 
      05-04-2007



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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hot Requirements: 1.Sr Java Developer,2.Java Developer (Java with EJB) Isaac Java 0 01-20-2011 08:41 PM
hey i am just started java,, can anyone tell me the use ,application, why java , importance of java.. manish sahu Java 3 02-14-2008 12:00 AM
[JAVA] [EVALUATION] - The Java Failure (Sorry: The Java(tm) Failure) Ilias Lazaridis Java 0 02-01-2005 10:32 AM
JAVA VIRTUAL MUCHINE OR SUN JAVA Fernando Kohan Firefox 1 11-14-2004 02:04 AM
Job to convert Java App 1.3.1 to Java Newest of Java Michael Kintner Java 0 11-30-2003 04:42 AM



Advertisments