Download presentation
Presentation is loading. Please wait.
Published byGinger Heath Modified over 9 years ago
1
Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004
2
What is a thread? A sequential flow of instructions Multiple threads are used to do multiple things at once.
3
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."); }
4
Timers run as separate threads By default, a program keeps running as long as its timer threads are running.
5
Creating a custom thread Extend the Thread class Override the run() method.
6
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()); }
7
How to start a thread public class ThreadsExample { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); }
8
A different way to create a thread Implement the Runnable interface Override the run() method.
9
Does it matter which you choose? Extend Thread Implement Runnable What if you are creating an applet?
10
Two ways to start a thread: myThread.start(); Thread newThread = new Thread(myThread); newThread.start();
11
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(); }
12
Just a quick note The following: MyClass mc = new MyClass(); mc.doSomething(); Is equivalent to this: (new MyClass()).doSomething();
13
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.
14
Selfish Threads Suppose we have the following: public int tick = 1; public void run() { while (tick < 10000000) tick++; }
15
Time sliced system Thread #1, tick = 50000 Thread #0, tick = 50000 Thread #0, tick = 100000 Thread #1, tick = 100000 Thread #1, tick = 150000 Thread #1, tick = 200000 Thread #0, tick = 150000 Thread #0, tick = 200000 Thread #1, tick = 250000 Thread #0, tick = 250000 Thread #0, tick = 300000 Thread #1, tick = 300000 Thread #1, tick = 350000 Thread #0, tick = 350000 Thread #0, tick = 400000 Thread #1, tick = 400000
16
Non-time-sliced system Thread #0, tick = 50000 Thread #0, tick = 100000 Thread #0, tick = 150000 Thread #0, tick = 200000 Thread #0, tick = 250000 Thread #0, tick = 300000 Thread #0, tick = 350000 Thread #0, tick = 400000 Thread #1, tick = 50000 Thread #1, tick = 100000 Thread #1, tick = 150000 Thread #1, tick = 200000 Thread #1, tick = 250000 Thread #1, tick = 300000 Thread #1, tick = 350000 Thread #1, tick = 400000
17
The solution: make ‘polite’ threads Periodically call the yield() method. For Example: public int tick = 1; public void run() { while (tick < 10000000) tick++; this.yield(); } Or perhaps: public int tick = 1; public void run() { while (tick < 10000000) tick++; if(tick % 1000 == 0) this.yield(); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.