Professional Documents
Culture Documents
MODULE: Threads: - Using Threads - Java - Lang.thread - Java - Lang.runnable - Sleep, Join, Yield
MODULE: Threads: - Using Threads - Java - Lang.thread - Java - Lang.runnable - Sleep, Join, Yield
• Using threads
• java.lang.Thread
• java.lang.Runnable
javaprogrammering
When use threads?
• If you have to wait for a resource or an event (such as user input)
• etc
javaprogrammering
Thread
• For separate, independently running subtasks
javaprogrammering
Process vs thread
• Process – a self-contained running program with its own
address space (e.g. running the JVM)
javaprogrammering
Thread scheduler
start() scheduler
t1 t6
t4 t2
t3 t5
javaprogrammering
Runnable
public interface Runnable {
public void run();
}
javaprogrammering
Yield, ex
class MyClass implements Runnable {
public void run() {
int i = 0;
while (i<100) {
Thread.yield();
System.out.print(” ”+i++);
}
}
}
javaprogrammering
Putting a thread to sleep, ex
class MyClass implements Runnable {
public void run() {
int i = 0;
while (i<100) {
try{
Thread.sleep(100);
} catch(InterruptedException ie){
ie.printStackTrace();
}
System.out.print(” ”+ (i++) );
}
}
}
Note: sleep() will put the thread to sleep, thereby giving other threads a
chance to run (cf. yield())
javaprogrammering
Blocked threads
scheduler
t1 running
runnable t6 threads
threads t4 t2
t3 t5
sleep()
javaprogrammering
Two ways…
Two ways to implement a thread:
javaprogrammering
Extending thread, ex
class MyClass extends Thread {
private String message;
MyClass(String message) {
implements Runnable,
this.message = message;
has methods like start() etc.
}
public void run() {
long t = System.currentTimeMillis();
try{ Thread.sleep(5000); }
catch(InterruptedException ie){
ie.printStackTrace();
}
System.out.print(message);
System.out.println(”time = ”+
(System.currentTimeMillis()-t));
}
}
javaprogrammering
Extending thread, ex
public class TestThread {
public static void main(String[] args){
Thread mc = new MyClass("hej");
mc.start();
}
}
javaprogrammering
Implementing Runnable, ex
class MyClass implements Runnable {
private String message;
MyClass(String message) {
this.message = message;
}
public void run() {
try{
Thread.sleep(5000);
} catch(InterruptedException ie){
ie.printStackTrace();
}
System.out.print(message);
}
}
javaprogrammering
Implementing Runnable, ex
public class TestThread {
public static void main(String[] args){
Runnable mc = new MyClass("hej");
Thread t = new Thread(mc);
t.start();
}
}
javaprogrammering
Implement Runnable or Extend Thread?
• Implementing Runnable:
– Better structure, better OO
– Maintains the possibility to extend
– Consistency in code
• Extending Thread:
– Slightly less code
– What about “is-a”…?
javaprogrammering
Interrupting a thread, ex
public static void main(String[] args) {
MyClass mc = new MyClass("hej");
Thread t = new Thread(mc);
t.start();
try{
Thread.sleep(2000);
} catch(InterruptedException ie){
ie.printStackTrace();
}
t.interrupt();
}
javaprogrammering
Waiting for a thread, ex
public static void main(String[] args) {
MyClass mc = new MyClass("hej");
Thread t = new Thread(mc);
t.start();
try{
t.join();
} catch(InterruptedException ie){
ie.printStackTrace();
}
System.out.println(“hopp!”);
}
(Example: The software for an ATM, where several checks have to be done
before we can proceed (card data, code, account balance etc.)
javaprogrammering
Activity diagram
Put card
in slot
Check ID Check
balance
Receive
Synchronization money
bar
javaprogrammering
Stopping a thread
•Use a boolean flag/condition to stop a thread
•Do not use stop() (or any other deprecated metod; see
API doc)
t.start();
t2.start();
}
javaprogrammering
Sharing code and data, ex
public static void main(String[] args) {
t.start();
t2.start();
}
javaprogrammering
Protecting data
• Use synchronized if more than one thread can modify
an object’s data
javaprogrammering
Stack, ex
class MyStack {
private[] cArr = new char[10];
private int i;
public void push(char c) {
cArr [i] = c;
i++;
}
public char pull() {
i--;
return cArr[i];
}
}
javaprogrammering
Stack, ex
class MyStack {
private[] cArr = new char[10];
private int i;
public void push(char c) {
synchronized(this) {
cArr [i] = c;
i++;
}
}
public synchronized char pull() {
i--;
return cArr[i];
}
}
javaprogrammering
wait() and notify()
public void push(char c) {
synchronized(this) {
cArr [i] = c;
i++;
this.notify();
}
}
javaprogrammering
This document was created with Win2PDF available at http://www.daneprairie.com.
The unregistered version of Win2PDF is for evaluation or non-commercial use only.