Multithreaded applets

Slides:



Advertisements
Similar presentations
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Advertisements

Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
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-
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.
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
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)
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 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
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.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
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.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Threading and Concurrency COM379T John Murray –
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© 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.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
Threads in Java Two ways to start a thread
Chapter 13: Multithreading
Java Multithreading.
Multithreading.
Threads and Multithreading
CSE 501N Fall ‘09 21: Introduction to Multithreading
Object-Orientated Analysis, Design and Programming
Chapter 19 Java Never Ends
Threads Chate Patanothai.
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
Threads and Multithreading
Threads and Multithreading
Java Based Techhnology
Principles of Software Development
Multithreading.
Multithreaded Programming
Threads and Multithreading
Threads and Multithreading
Java Threads (Outline)
Java Threads (Outline)
Unit 1 Lab14 & Lab15.
Threads and Multithreading
Java Thread.
Chapter 15 Multithreading
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Representation and Management of Data on the Internet
Threads.
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multithreaded applets Java threading Multithreaded applets Peter Mozelius DSV/UCSC

Robustness An applet should not crash and hang the browser A program should handle its errors Built in error handling in Java try catch finally

Catching exceptions try – catch - finally try { Do something useful that sometimes can go wrong and throw an Exception; } catch(Exception e) { Here will the exeption be caught; finally { Will always be executed;

Threading A technique for dividing the execution of an applet in parallel tasks As when several instruments play in parallel in an orchestra With synchronized interaction and pauses between the threads

Threading Parallel code execution Multitasking Multithreading To run several processes in parallel Multithreading To run several threads in parallel

Java threads Thread = lightweight process Since the very first ver 1.0 in Java To save system resources Instead of starting a new process Start a new thread Thread = lightweight process

Why threading? For practical and economical reasons Example: A web browser could fetch several images in parallell An applet could show an animations in separate threads, at the same time as the main thread will handle the GUI

How to make threads sleep So other threads should be able to execute their tasks the thread must sometime sleep for a while We will now look at how you should make threads rest Do we need to rest as well?

How threads sleep Threads will be put in bed by sleep() As an argument this method will need the number of milliseconds to sleep Example: sleep(1000); Makes the thread sleep exactly 1 second

Exception handling To avoid thread trouble: try{ sleep(millisec); }catch(InterruptedException ie){ System.out.print(”Thread error: ”); System.out.print(ie.getMessage()); }

Starting and stopping threads If you create a thread like: Thread t = new Thread(); You’ll later start it by: t.start();

Starting and stopping threads In the class Thread there is also a method named stop() BUT do NOT stop the thread by stop() This method is deprecated because it leaves the thread in an unclear condition

suspend() och resume() Two other ways of stopping and restarting threads are by: suspend() och resume() BUT these methods are deprecated as well because they create deadlocks And they are NOT recommended

How the threads work Threads execute their tasks in run() If the iteration in a run-method is trigged by a boolean condition in a loop The thread will always be able to finish it’s work in a secure manner

A stop condition in run() Create a boolean condition like: private boolean stopped; public void run(){ while(!stopped) { moveTheBall(); }

Thread and Runnable The two ways of creating threads in Java: By subclassing Thread OneClass extends Thread By implementing Runnable AnotherClass implements Runnable

Runnable The fact that Runnable is an interface can be very useful when a class inherits something else like in: MyClass extends JApplet implements Runnable We will now look at some code but first: 15 min PAUS!

Let’s have a break!

Threading with Thread class TestThread extends Thread { private String name; private int sleepTime; public TestThread(String name, int sleepTime) { this.name = name; this.sleepTime = sleepTime; }

Threading with Thread System.out.println("Hello, from : " + name); public void run() { System.out.println("Hello, from : " + name); try { sleep(sleepTime); }catch (InterruptedException ie) { System.err.println(“ERROR in " + name); System.err.println(ie.getMessage()); } System.out.println("Hello again, from: " + name); }//run }//TestThread

Threading with Thread Threads can then be created from an application: (or an applet) public class L4_example1 { public static void main(String[] args) { TestThread t1 = new TestThread(“T1", 2000); TestThread b2 = new TestThread(“T2", 20); t1.start(); t2.start(); } }//L4_example1

Threading with Runnable class MyThread implements Runnable { private String threadName; private int sleepTime; private Thread t; public MyThread(String name, int time) { threadName = name; sleepTime = time; t = new Thread(this); }

Threading with Runnable public void run() { System.out.println("Hello, from: " + name); int i = 0; while (i++ < 2) { try { t.sleep(sleepTime); } catch (InterruptedException ie) { System.err.println(“ERROR in " + name); System.err.println(ie.getMessage());

Threading with Runnable System.out.println("Hello again, from: " + threadName); }//while }//run public void startMe() { t.start(); } }//MyThread

Threading with Runnable public class L4_example2 { public static void main(String[] args) { MyThread t1 = new MyThread("T1", 2000); MyThread t2 = new MyThread("T2", 20); t1.startMe(); t2.startMe(); } }//L4_example2

Graphics in threaded classes If two diffrent thread classes want to write to the same graphical component:

Graphics in threaded classes A reference to the graphical component can be passed to the constructor in the thread class : class PenThread extends Thread { private JPanel centerPanel ; public PenThread(JPanel centerPanel) { this.centerPanel = centerPanel; ...

Graphics in threaded classes Then you can write on the panel by: public void write() { Graphics pen = centerPanel.getGraphics(); pen.drawString("Hello, from: " + threadName, x, y); pen.dispose(); }

Graphics in threaded classes The complete code for L4_example3 can be found in the Moodle system An example that could be a good start for solving Assignment 7 So let’s run this applet!