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.

Slides:



Advertisements
Similar presentations
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Advertisements

1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
11-Jun-15 Producer-Consumer An example of using Threads.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
1 Thread Pools Representation and Management of Data on the Internet.
Object-Oriented Software Engineering Concurrent Programming.
16-Jun-15 Producer-Consumer An example of using Threads.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
26-Jun-15 Threads and Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread extends.
28-Jun-15 Producer-Consumer An example of using Threads.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Threads Just Java: C10–pages 251- C11–pages 275-
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Internet Software Development More stuff on Threads Paul Krause.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threads some important concepts Simon Lynch
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
Threading Eriq Muhammad Adams J
Lecture 20: Parallelism & Concurrency CS 62 Spring 2013 Kim Bruce & Kevin Coogan CS 62 Spring 2013 Kim Bruce & Kevin Coogan Some slides based on those.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Advanced Programming 2004, based on LY Stefanus’s slides slide 8.1 Multithreading : Thread Scheduling ThreadGroup.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Chaining & Threading Steve Chenoweth Office: Moench Room F220 Phone: (812)
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Multi-Threading in Java
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Distributed and Parallel Processing George Wells.
SCJP 9/10 Threads.
Threads and Multithreading
Practice Session 8 Lockfree LinkedList Blocking queues
Threads Chate Patanothai.
Threads II IS
Condition Variables and Producer/Consumer
Threads and Multithreading
Cancellation.
Condition Variables and Producer/Consumer
Threads and Multithreading
Multithreading 2 Lec 24.
Threads and Multithreading
Threads and Multithreading
Unit 1 Lab14 & Lab15.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads.
Software Engineering and Architecture
Presentation transcript:

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 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();

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

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"; } } }

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”

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(); } }

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)); } } }

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 $ /17/