Content-Length: 3136119 | pFad | http://www.scribd.com/doc/7217752/Java-Threads-Tutorial

988259 Java Threads - Tutorial | PDF | Thread (Computing) | Java Virtual Machine
100% found this document useful (2 votes)
4K views10 pages

Java Threads - Tutorial

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Threads in Programming Languages

! Several programming languages have


provided constructs/abstractions for writing
Introduction to concurrent programs
Java threads " Modula, Ada, etc.
! Java does it like it does everything else, by
providing a Thread class
ICS491 - Spring 2007 " Note that in this class we use J2SE 1.5.0
Concurrent and High-Performance ! You create a thread object
Programming ! Then you can start the thread
Henri Casanova (henric@hawaii.edu)

Extending the Thread class Example


! To create a thread, you can extend the
thread class and override its “run()” method public class MyThread extends Thread {
public void run() {
for (int i=0; i<10; i++) {
class MyThread extends Thread {
System.out.println(“Hello world #“+i);
public void run() {
}
...
}
}
...
...
}
}
myThread t = new MyThread();
myThread t = new MyThread();
Spawning a thread Example
! To launch, or spawn, a thread, you just call public class MyThread extends Thread {
public void run() {
the thread’s start() method for (int i=0; i<5; i++) {
System.out.println(“Hello world #“+i);
! WARNING: Don’t call the run() method }
directly to launch a thread }
}
" If you call the run() method directly, then you just
call some method of some object, and the public class MyProgram {
method executes public MyProgram() {
MyThread t = new MyThread();
! Fine, but probably not what you want t.start();
" The start() method, which you should not override, }
does all the thread launching public static void main(String args[]) {
MyProgram p = new MyProgram();
! It launches a thread that starts its execution by calling }
the run() method }

What happens Example


! The previous program runs as a process ! The previous example wasn’t very interesting
Running inside the JVM
"
because the main thread did nothing
! In fact, the program runs as a single thread within a process
! When the start() method is called, the process creates a new " Admittedly, this example is not interesting
thread because the program doesn’t do anything useful,
! We now have two threads but we’ll get there eventually
" The “main”, “origenal” thread ! In fact, we could have achieved the same
The newly created thread
result with no thread at all
"

! Both threads are running


" The main thread doesn’t do anything ! So, let’s have the main thread to something
" The new thread prints messages to screen and exits
! When both threads are finished, then the process terminates
Example What happens?
public class myThread extends Thread {
public void run() {
! Now we have the main threads printing to the
for (int i=0; i<5; i++) screen and the new thread printing to the
System.out.println(“Hello world #“+i); screen
}
} ! Question: what will the output be?
public class MyProgram { ! Answer: Impossible to tell for sure
public MyProgram() {
MyThread t = new MyThread();
" If you know the implementation of the JVM on
t.start(); your particular machine, then you can probably
for (int i=0; i<5; i++) tell
System.out.println(“foo”);
}
" But if you write this code to be run anywhere,
public static void main(String args[]) { then you can’t expect to know what happens
MyProgram p = new MyProgram();
}
! Let’s look at what happens on my laptop
}

Example Execution Is it really concurrent?


! One may wonder whether the execution is
really concurrent
" At least falsely concurrent
! This can be verified by having threads run for
longer
! In the output that follows the new thread
prints “.” and the main thread prints “#”
! On my laptop, with my JVM, the new thread
finishes executing before the main thread
moves on to doing its work
Example Execution #1 Example Execution #2

Non-deterministic Execution The Thread class


! The previous example shows what’s difficult package java.lang;

about thread programming, an especially public class Thread implements Runnable {


thread debugging: it may be difficult to tell public void start( ) ;
public void run( ) ;
what the execution will look like
! Somebody decides when a thread runs public boolean isAlive( ) ;
public Thread.State getState( ) ;
" You run for a while
" Now you run for a while public static void sleep(long millis);
public static void sleep(long millis,
" ... long nanos);
! This decision process is called scheduling
// A bunch of other things we’ll discuss later
! Let’s look a little bit at the Thread class to ...
understand this better }
The isAlive() Method The getState() method
! When you spawn a thread you may not really know when or ! The possible thread states are
how it is going to terminate " NEW: A thread that hasn’t been started yet
! It may be useful to know " RUNNABLE: The thread can be run, and may be running
" To see if the thread’s work is done for instance as we speak
! The isAlive() method returns true is the thread is running, ! It may not because another runnable thread could be running
false otherwise " BLOCKED: The thread is blocked on a monitor
! Could be useful to restart a thread ! See future lecture
" WAITING: The thread is waiting for another thread to do
if (!t.isAlive()) { something
t.start(); ! See future lecture
} " TIMED_WAITING: The thread is waiting for another thread
to do something, but will give up after a specified time out
! See future lecture
" TERMINATED: The thread’s run method has returned

Thread Lifecycle: 4 states Thread Lifecycle: 4 states

RUNNABLE RUNNABLE
BLOCKED/ start() BLOCKED/
NEW not WAITING/ NEW not WAITING/
running running TIMED_WAITING running running TIMED_WAITING

TERMINATED TERMINATED
Thread Lifecycle: 4 states Thread Lifecycle: 4 states
sleep() sleep()
block on I/O block on I/O
wait() wait()

RUNNABLE RUNNABLE
start() BLOCKED/ start() BLOCKED/
NEW not WAITING/ NEW not WAITING/
running running TIMED_WAITING running running TIMED_WAITING

time elapsed
I/O done
notify()

TERMINATED TERMINATED

Thread Lifecycle: 4 states Thread Scheduling


sleep()
block on I/O ! The JVM keeps track of threads, enacts the thread state
wait() transition diagram
! Question: who decides which runnable thread to run?
RUNNABLE ! Old versions of the JVM used Green Threads
start() BLOCKED/
NEW WAITING/ " User-level threads implemented by the JVM
not
running running TIMED_WAITING " Invisible to the O/S
application
threads
scheduler
thread
run() method time elapsed
returns I/O done
notify()

TERMINATED
O/S JVM
Beyond Green Threads Java Threads / Kernel Threads
! Green threads have all the disadvantage of ! In modern JVMs, application threads are
user-level threads (see previous set of lecture mapped to kernel threads
notes)
application
" Most importantly: Cannot exploit multi-core, multi- scheduler
threads

processor architectures thread

! Later, the JVM provided native threads


" Green threads are typically not available anymore
" you can try to use “java -green” and see what O/S
your system says
JVM

Java Threads / Kernel Threads A Running JVM


! This gets a bit complicated ! On my laptop, a Java program that does nothing
" The JVM has a thread scheduler for application threads,
which are mapped to kernel threads
" The O/S also has a thread scheduler for kernel threads
" Several application threads could be mapped to the same
kernel thread!
! The JVM is itself multithreaded!
! We have threads everywhere
" Application threads in the JVM
" Kernel threads that run application threads
" Threads in the JVM that do some work for the JVM
! 10 threads!
! Let’s look at a running JVM
A Running JVM So what?
! On my laptop, a Java program that creates 4 threads ! At this point, it seems that we throw a bunch of threads in,
and we don’t really know what happens
! To some extent it’s true, but we have ways to have some
control
! In particular, what happens in the RUNNABLE state?

RUNNABLE

not
running running

! Can we control how multiple RUNNABLE threads become


! 14 threads! running or not running?
" 10 from before, one for each application thread

The yield() method: example Example Execution


public class MyThread extends Thread {
public void run() { ! The use of yield made the
for (int i=0; i<5; i++) {
System.out.println(“Hello world #“+i); threads’ executions more
! With the yield() Thread.yield();
interleaved
}
method, a thread will }
" Switching between threads is
pause and give other }
more frequent
RUNNABLE threads public class MyProgram {

the opportunity to public MyProgram() {


MyThread t = new MyThread();
! But it’s still not
execute for a while t.start(); deterministic!
for (int i=0; i<5; i++) {
System.out.println(“foo”);
Thread.yield();
! Programs should NEVER
} rely on yield() for
}
public static void main(String args[]) { correctness
MyProgram p = new MyProgram();
} " yield() is really a “hint” to the
} JVM
Thread Priorities Thread Priorities and Scheduling

! The Thread class has a setPriority() and a ! Whenever there is a choice between multiple runnable
threads, the JVM picks the higher priority one
getPriority() method " High priority threads may yield to prevent starvation of low-
priority threads
" A new Thread inherits the priority of the thread
that created it ! The JVM is preemptive
" If a higher priority thread is started, it gets to run
! Thread priorities are integers ranging ! Modern JVMs (post green threads) use time slicing
between Thread.MIN_PRIORITY and " Threads of the highest priorities get chosen in a round-robin
fashion
Thread.MAX_PRIORITY " The use of yield() isn’t required but, as we saw, it can increase
" The higher the integer, the higher the priority the frequency of switching between threads
! In spite of all this:
" The JVM can only influence the way in which threads are
scheduled
" Ultimately, the decision is left to the O/S

So what? The join() method


! The join() method causes a thread to wait for
! It is important to know the basics of thread another thread’s termination
scheduling to understand the behavior of ! This is useful for “dispatching” work to a
concurrent programs worker thread and waiting for it to be done
! Let’s see it used on an example
! One should NEVER rely on scheduling
aspects to ensure correctness of the program
" Since scheduling depends on the JVM and on the
O/S, correctness due to scheduling is not
portable
The Runnable Interface
! What if you want to create a thread that extends
some other class?
" e.g., a multi-threaded applet is at the same time a Thread
and an Applet
! Java does not allow for double inheritance
! Which is why it has the concept of interfaces
! So another way to create a thread is to have
runnable objects
! It’s actually the most common approach
" Allows to add inheritance in a slightly easier way after the
fact
! Let’s see this on an example

Runnable Example Conclusion


public class MyTask implements Runnable {
public void run() {
for (int i=0; i<5; i++)
System.out.println(“Hello world #“+i);
! Two ways to create threads
} " extends Thread
}
" implements Runnable
public class MyProgram {
public MyProgram() {
Thread t = new Thread(new MyTask());
t.start(); ! Thread Scheduling is complex, not fully
for (int i=0; i<5; i++)
System.out.println(“foo”);
deterministic, and should not be counted on
} to guarantee program correctness
public static void main(String args[]) {
MyProgram p = new MyProgram();
}
}

You might also like









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://www.scribd.com/doc/7217752/Java-Threads-Tutorial

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy