Query:multithread about java

J

Jack Dowson

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

Ramesh

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
 
J

Jack Dowson

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

Lew

Jack said:
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();
}
}
 
J

Jack Dowson

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

Ramesh

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top