8
Threads and Taking Turns

23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

Embed Size (px)

Citation preview

Page 1: 23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

Threads and Taking Turns

Page 2: 23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

Thread review

There are two ways to create a Thread object.. Extend Thread and supply a run method:

class MyThread extends Thread { public void run( ) { ... }}

Thread someThread = new MyThread(); Implement Runnable:

class Something implements Runnable { public void run( ) { ... }}

Something something = new Something(); Thread someThread = new Thread(something);

...And just one way to start a Thread running someThread.start();

Page 3: 23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

Implementing a Queue

This kind of Queue is used for passing objects from one Thread to another The Queue has to be synchronized, because having two

different Threads modifying it “at the same time” could be disastrous

If a Thread wants to get something from the Queue, and the Queue is empty, it has to wait for something to be put in the Queue

That is, get is a blocking operation

In the following code, I’ll use an ArrayList to implement the Queue This is not as efficient as it could be (O(n)), but it’s fast

enough for my purposes

Page 4: 23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

Queue code

import java.util.ArrayList;

public class Queue {

ArrayList list = new ArrayList(); public void put(Object obj) { synchronized (list) { list.add(obj); list.notify(); } }

public Object get() { if (list.size() > 0) { synchronized (list) { return list.remove(0); } } else try { synchronized (list) { list.wait(); return get(); } } catch (InterruptedException e) { return "Interrupted"; } }}

Page 5: 23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

Two-way communication

A Queue is fine if one class is a producer and the other is a consumer

However, what if one Thread wants to ask questions of the other Thread?

Thread 1: putRequest(request): response = getResponse();

Thread 2: request = getRequest(); response = doSomethingWith(request); putResponse();

This requires a pair of Queues In my code, I call this a “Channel”

Page 6: 23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

Channel (a pair of Queues) public class Channel {

Queue request = new Queue(); Queue response = new Queue(); public void putRequest(Object obj) { request.put(obj); } public Object getRequest() { return request.get(); }

public void putResponse(Object obj) { response.put(obj); }

public Object getResponse() { return response.get(); }}

Page 7: 23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

Turn-based play Suppose I want to accept and execute “commands” from several players, ending with

an “action” command

Channel[] channels; // One for each player

public void run() { Command request; Object response;

while (true) { for (int i = 0; i < channels.length; i++) { do {

request = (Command) channels[i].getRequest(); response = doCommand(request); channels[i].putResponse(response);

} while (!isAction(request)); } }}

Page 8: 23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread

The End

bbbbryan writes to tell us about the commercialization of the elusive alarm clock prototyped at the MIT Media Lab a couple of years back. This alarm clock actually runs, hides from you, and beeps to ensure that you'll be awake enough not to go back to sleep by the time you find it and get it shut up. Detroit News has a writeup on the device, which you can buy from the inventor's site for $50. -- http://slashdot.org/ 4/17/2006

-- http://www.nandahome.com/