Professional Documents
Culture Documents
Java Threading
Java Threading
Chapter 15
Multithreading
thread
The basic unit of program execution.
http://java.sun.com/docs/books/tutorial/essential/threads/index.html
Threading
concurrent
“threads of
*
execution”
The “language” C# (C-pound) has been alleged to “support” a form of multi-threading.
Java II--Copyright © 2001-2002 Tom Hunter
15.1 Introduction: priority
• Since most computers have only one CPU, it is a myth
that separate threads run simultaneously.
timeslicing.
java.lang.Thread
Java II--Copyright © 2001-2002 Tom Hunter
Thread()—this constructor takes no
arguments.
String n = “cece”;
Thread t2 = new Thread( n );
String n = “cece”;
Thread t3 = new Thread( n );
Thread t4 = new Thread();
run()
This method takes no arguments and returns void.
}
} Java II--Copyright © 2001-2002 Tom Hunter
public class MyThread extends Thread
{
public MyThread( String n )
{
super( n );
System.out.println( “MyThread Constructor” );
}
public void run()
{
System.out.println( “In run!\n” );
} So, will we already be running
a thread in main now?
public class TestMyThread
{
public static void main( String[] args )
{ MyThread chuck = new MyThread( "Charlie" );
chuck.start();
MyThread dede = new MyThread( ”Deirdra" );
dede.start();
MyThread pat = new MyThread( “Patrick" );
pat.start();
main() will instantiate and start() all these threads. After they have
started, the methods immediately return and the thread main() dies.
} However the entire application doesn’t die until the last thread does.
} Java II--Copyright © 2001-2002 Tom Hunter
public class MyThread extends Thread chuck=Thread[Charlie,5,main]
{ dede=Thread[Deirdra,5,main]
public MyThread( String n ) pat=Thread[Patrick,5,main]
{
super( n );
System.out.println( “In Constructor” );
•Thread name
} • priority
public void run()
{
• thread groups
System.out.println( “In run!\n“ );
} By default, a new thread has the
}
public class TestMyThread same
{ priority as the thread that created it.
public static void main( String[] args )
{
MyThread chuck = new MyThread( “Charles” );
chuck.start();
MyThread dede = new MyThread( “Deirdra” );
dede.start();
MyThread pat = new MyThread( “Patrick” );
pat.start();
System.out.println( "chuck=" + chuck.toString() );
System.out.println( " dede=" + dede.toString() );
System.out.println( " pat=" + pat.toString() );
}
}
Java II--Copyright © 2001-2002 Tom Hunter
Notice what
happened here—in
our main method, MyThread Constructor
In run!
we instantiated our
MyThread class. MyThread Constructor
In run!
Then, we executed the
start() method of our MyThread Constructor
object. In run!
We never called the chuck=Thread[Charlie,5,main]
run() method but, dede=Thread[Deirdra,5,main]
lo and behold,
behold it got pat=Thread[Patrick,5,main]
called.
4
main
chuck,
dede,
pat
When you instantiate a new thread object, and then execute its
start() method, you are asking the JVM to create a new,
additional thread to run alongside the thread you created when
your original main() method started up.
Java II--Copyright © 2001-2002 Tom Hunter
Although you have a method run() in your MyThread
class, don’t ever execute the run() method yourself. Let
the JVM do it for you after you execute the start()
method.
public static void main( String[] args )
{
MyThread mt = new MyThread();
mt.start();
mt.run();
}
In run()-thread=Thread[Deirdra,5,main]
In run()-thread=Thread[Patrick,5,main]
}
Java II--Copyright © 2001-2002 Tom Hunter
public class MyThread extends Thread
{
public MyThread( String name )
{
super( name );
}
public run() In run()-thread=Thread[Charlie,5,main]
{
System.out.println( “In run()-thread=“ + Thread.currentThread() );
} In run()-thread=Thread[Deirdra,5,main]
} In run()-thread=Thread[main,5,main]
In run()-thread=Thread[Patrick,5,main]
IllegalThreadStateException
current thread.
Is a certain named
thread interrupted?
boolean isInterrupted()
void interrupt()
Java II--Copyright © 2001-2002 Tom Hunter
15.2 A Survey of the Thread Methods
boolean isInterrupted()
void wait()
boolean isAlive()
String getName()
• blocked
• sleeping
• waiting
thread synchronization.
ii.) I Produce the 35 pounds of peaches, weigh them and write down
the total bill on a chalk slate.
iii.) My partner Luigi bags the peaches, reads the price on the slate
and takes the money.
iv.) But I’m much faster than Luigi, so by the time he gets the
peaches bagged, I’ve already erased the slate and put the next price
down there, which was for 5 pounds of peaches.
ii.) I Produce the 35 pounds of peaches, weigh them and write down
the total bill on a chalk slate.
iii.) My partner Luigi bags the peaches, reads the price on the slate
and takes the money.
vi.) Result: Consumer gets annoyed because of the delay but we stay
in business because our operations are synchronized.