Public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread.

Slides:



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

Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Albert Johnson Molly Richardson Andrew Kruth.  Threads Runnable interface sleep()  GUIs repaint() paintComponent()
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
Multithreading Why multi-threads Defining and creating threads An example with two threads Life cycle of a thread An example with user interface Problem.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part II.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
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.
Unit 151 Threads II Thread Priorities The interrupt() method Threads Synchronization Thread Animation Examples – after each section Exercises.
Thread Examples. Runnable Interface Runnable defines only one abstract method; Public void run(); Thread also implements Runnable interface. Why does.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
24-Jun-15 Simple Animation. 2 The bouncing ball The “bouncing ball” is to animation what “Hello world” is to programming With a single Thread, we can.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Programming in Java; Instructor:Alok Mehta Threads1 Programming in Java Threads.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Java Threads CS Introduction to Operating Systems.
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.
Unit 151 Introduction to Threads and Concurrency Introduction to Threads Multithreading Examples Life-cycle of a Thread Thread Priorities More Examples.
Multithreading.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
 Traditionally, a process in an operating system consists of an execution environment and a single thread of execution (single activity).  However,
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
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)
10/17/2015Vimal1 Threads By 10/17/2015Vimal2 Why use threads?? It is a powerful programming tool Computer users take it for granted.
Week 3, Day 1: Processes & Threads Processes Threads SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
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.
Multithreading in JAVA
Threading and Concurrency COM379T John Murray –
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Hints on debugging
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Multithreaded applets
Chapter 13: Multithreading
Lecture 9 Object Oriented Programming Using Java
Multithreading in Java
Chapter 19 Java Never Ends
Threads Chate Patanothai.
Multithreaded Programming in Java
Multithreading 2 Lec 24.
Principles of Software Development
Multithreading.
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
class PrintOnetoTen { public static void main(String args[]) {
Java Thread.
Chapter 15 Multithreading
Representation and Management of Data on the Internet
Philosopher Example class PhilosophersShare {
Presentation transcript:

public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread { public void start() { } public void run() { System.out.print(“Bye \n”) } public class TestThreads { public static void main(String[] args) { MyThread1 x = new MyThread1(); MyThread2 y = new MyThread2(); x.start(); // DO NOT CALL run()!!! y.start(); // DO NOT CALL run()!!! }

public class MyThread1 extends Thread { public void start() { } public void run() { for(int i = 0; i < 100; i++) System.out.print(“Hello \n”) } public class MyThread2 extends Thread { public void start() { } public void run() { for(int i = 0; i < 100; i++) System.out.print(“Bye \n”) } public class TestThreads { public static void main(String[] args) { MyThread1 x = new MyThread1(); MyThread2 y = new MyThread2(); x.start(); // DO NOT CALL run()!!! y.start(); // DO NOT CALL run()!!! }

run() MyThread1 MyThread2 CPU Step1: Execute 4 lines Then stop the execution Step2: Execute 3 lines then Stop the execution CPU collaborates with the JVM during Execution. The JVM schedules the jobs To be done by the CPU : JVM has a sub-program called: SCHEDULER

Panel 1 Panel 2 Main Frame Each ball is re-drawn after 1 sec at a new random location within its own panel! Start Ball Bouncing THREADED GUI-BASED APPLICATION 1 JButton

0 Main Frame User clicks on Start then the textfield increments each second by 1! Start THREADED GUI-BASED APPLICATION 2 Textfield JButton

1003 Main Frame User clicks on Pause then the textfield stops incrementing Pause THREADED GUI-BASED APPLICATION 2 (Cont’d)

public class ClassToRun extends SomeClass implements Runnable { public void run() { // Fill this as if ClassToRun // were derived from Thread }... public void startThread() { // Just to start the thread!!! ClassToRun ref = new ClassToRun(); Thread theThread = new Thread(ref); theThread.start(); }... }

public void meth1() { try { // Thread.sleep(1000); // Stop execution for 1 sec!!!! } catch(InterruptedException iec) { System.exit(-1); // SOMETHING IS WRONG!!! }