












































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













































































What Are Threads?
Ball ball = new Ball(); panel.add(ball);
for (int i = 1; i <= STEPS; i++) { ball.move(panel.getBounds()); panel.paint(panel.getGraphics()); Thread.sleep(DELAY); }
private BallPanel panel;
public static final int DEFAULT_WIDTH = 450;
public static final int DEFAULT_HEIGHT = 350;
public static final int STEPS = 1000;
public static final int DELAY = 3;
}
- static void sleep(long millis) public interface Runnable { void run(); }
class MyRunnable implements Runnable { public void run() { task code } }
Runnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
class BallRunnable implements Runnable {
...
public void run() { try { for (int i = 1; i <= STEPS; i++) { ball.move(component.getBounds()); component.repaint(); Thread.sleep(DELAY); } } catch (InterruptedException exception) { } }
... }
Ball b = new Ball(); panel.add(b); Runnable r = new BallRunnable(b, panel); Thread t = new Thread(r); t.start();
public void run()
{
try
{
for (int i = 1; i <= STEPS; i++)
{
ball.move(component.getBounds());
component.repaint();
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
}
}
private Ball ball;
private Component component;
public static final int STEPS = 1000;
public static final int DELAY = 5;
}
/**
A ball that moves and bounces off the edges of a
rectangle
*/
class Ball
{
/**
Moves the ball to the next position, reversing direction
if it hits one of the edges
*/
public void move(Rectangle2D bounds)
{
x += dx;
y += dy;
if (x < bounds.getMinX())
{
x = bounds.getMinX();
dx = -dx;
}
if (x + XSIZE >= bounds.getMaxX())
{
x = bounds.getMaxX() - XSIZE;
dx = -dx;
}
if (y < bounds.getMinY())
{
y = bounds.getMinY();
dy = -dy;
}
if (y + YSIZE >= bounds.getMaxY())
{
y = bounds.getMaxY() - YSIZE;
- Thread(Runnable target) Interrupting Threads
while ( !Thread.currentThread().isInterrupted() && more work to do ) { do more work }
public void run() { try {
...
while (!Thread.currentThread().isInterrupted() && more work to do ) { do more work } } catch(InterruptedException e) { // thread was interrupted during sleep or wait } finally { cleanup, if required } // exiting the run method terminates the thread }
public void run() { try {
... while ( more work to do ) { do more work Thread.sleep( delay ); } } catch(InterruptedException e) { // thread was interrupted during sleep or wait } finally { cleanup, if required } // exiting the run method terminates the thread }
CAUTION: When the sleep method throws an InterruptedException, it also clears the interrupted status.
NOTE: There are two very similar methods, interrupted and isInterrupted. The interrupted method is a static method that checks whether the current thread has been interrupted. Further- more, calling the interrupted method clears the interrupted status of the thread. On the other hand, the isInterrupted method is an instance method that you can use to check whether any thread has been interrupted. Calling it does not change the interrupted status.
NOTE: The runnable state has nothing to do with the Runnable interface.
block on I/O
wait
dead
new
runnable
sleep
done sleeping
suspend
resume
notify
I/O complete
run method exits
start
blocked
stop
wait for lock
lock available
CAUTION: If you do use priorities, you should be aware of a common beginner’s error. If you have several threads with a high priority that rarely block, the lower-priority threads may never execute. Whenever the scheduler decides to run a new thread, it will choose among the high- est-priority threads first, even though that may starve the lower-priority threads completely.
t.setDaemon(true);
String groupName =.. .; ThreadGroup g = new ThreadGroup(groupName)
Thread t = new Thread(g, threadName);
if (g.activeCount() == 0) { // all threads in the group g have stopped }
g.interrupt(); // interrupt all threads in group g