Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004
What is a thread? A sequential flow of instructions Multiple threads are used to do multiple things at once.
Example import java.util.Timer; import java.util.TimerTask; class MyTask extends TimerTask { public void run() { System.out.println("Time's up!"); TimerExample.timer.cancel(); // Terminate the timer thread } public class TimerExample { public static Timer timer; public static void main(String args[]) { System.out.println("About to schedule task."); timer = new Timer(); timer.schedule(new MyTask(), 5*1000); System.out.println("Task scheduled."); }
Timers run as separate threads By default, a program keeps running as long as its timer threads are running.
Creating a custom thread Extend the Thread class Override the run() method.
Example public class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { sleep((long)(Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + getName()); }
How to start a thread public class ThreadsExample { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); }
A different way to create a thread Implement the Runnable interface Override the run() method.
Does it matter which you choose? Extend Thread Implement Runnable What if you are creating an applet?
Two ways to start a thread: myThread.start(); Thread newThread = new Thread(myThread); newThread.start();
Implementing Runnable class SimpleThread implements Runnable { Thread newThread; public void start() { newThread = new Thread(this); newThread.start(); } public void run() { // do some stuff } public void stop() { newThread.stop(); }
Just a quick note The following: MyClass mc = new MyClass(); mc.doSomething(); Is equivalent to this: (new MyClass()).doSomething();
Priority Most Computers have only 1 CPU. Set priority with setPriority() Using integers ranging between Thread.MIN_PRIORITY and Thread.MAX_PRIORITY. Usually the thread with the highest priority is running.
Selfish Threads Suppose we have the following: public int tick = 1; public void run() { while (tick < ) tick++; }
Time sliced system Thread #1, tick = Thread #0, tick = Thread #0, tick = Thread #1, tick = Thread #1, tick = Thread #1, tick = Thread #0, tick = Thread #0, tick = Thread #1, tick = Thread #0, tick = Thread #0, tick = Thread #1, tick = Thread #1, tick = Thread #0, tick = Thread #0, tick = Thread #1, tick =
Non-time-sliced system Thread #0, tick = Thread #0, tick = Thread #0, tick = Thread #0, tick = Thread #0, tick = Thread #0, tick = Thread #0, tick = Thread #0, tick = Thread #1, tick = Thread #1, tick = Thread #1, tick = Thread #1, tick = Thread #1, tick = Thread #1, tick = Thread #1, tick = Thread #1, tick =
The solution: make ‘polite’ threads Periodically call the yield() method. For Example: public int tick = 1; public void run() { while (tick < ) tick++; this.yield(); } Or perhaps: public int tick = 1; public void run() { while (tick < ) tick++; if(tick % 1000 == 0) this.yield(); }