Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004.

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Advertisements

Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Object-Oriented Software Engineering Concurrent Programming.
Advanced Java Class Threads.
Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 37: Beyond Sequential Programs COMP 144 Programming Language Concepts Spring 2002.
Thread Examples. Runnable Interface Runnable defines only one abstract method; Public void run(); Thread also implements Runnable interface. Why does.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
Java Threads CS Introduction to Operating Systems.
Java, Java, Java Object Oriented Problem Solving Chapter 13: Threads and Concurrent Programming.
„Threads”. Threads Threads - basics A thread is a single sequential flow of control within a program. A thread itself is not a program; it cannot run.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
1 Java Threads Instructor: Mainak Chaudhuri
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
Threads.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
1 G53SRP: Clocks and Time in Java Chris Greenhalgh School of Computer Science.
Threads. Story so far  Our programs have consisted of single flows of control Flow of control started in the first statement of method main() and worked.
Threading and Concurrency COM379T John Murray –
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
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:
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
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.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Многопоточность в Java
Multithreading / Concurrency
Multithreading Lec 23.
Chapter 13: Multithreading
Multi Threading.
Multithreading.
Multithreaded Programming in Java
THREADS.
Threads Chate Patanothai.
Java Based Techhnology
Multithreading.
13: Concurrency Definition:  A thread is a single sequential flow of control within a program. Some texts use the name lightweight process instead of thread.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 15 Multithreading
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Representation and Management of Data on the Internet
Threads.
Gentle Introduction to Threads
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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