Representation and Management of Data on the Internet Java Threads Representation and Management of Data on the Internet
Threads: Objects with an own line of execution Main Thread1 Thread2 Thread3
Multitasking and Multithreading Multitasking refers to a computer's ability to perform multiple jobs concurrently more than one program are running concurrently, e.g., UNIX A thread is a single sequence of execution within a program Multithreading refers to multiple threads of control within a single program each program can run multiple threads of control within it, e.g., Web Browser
Concurrency vs. Parallelism CPU CPU1 CPU2
Threads and Processes CPU Process 1 Process 2 Process 3 Process 4 main run GC
What are Threads Good For? To maintain responsiveness of an application during a long running task. To enable cancellation of separable tasks. Some problems are intrinsically parallel. To monitor status of some resource (DB). Some APIs and systems demand it: Swing.
Application Thread When we execute an application: The JVM creates a Thread object whose task is defined by the main() method It starts the thread The thread executes the statements of the program one by one until the method returns and the thread dies
Multiple Threads in an Application Each thread has its private run-time stack If two threads execute the same method, each will have its own copy of the local variables the methods uses However, all threads see the same dynamic memory (heap) Two different threads can act on the same object and same static fields concurrently
Creating Threads There are two ways to create our own Thread object Subclassing the Thread class and instantiating a new object of that class Implementing the Runnable interface In both cases the run() method should be implemented
Implementing Threads The most simple way: extending the Thread class and overwriting the run() method Thread is an existing class (no need to import it) Has a run () method (originally without instructions) whose instructions can be run in parallel to what is currently running For that the start() method of and instance of the new class should be called Declaration of the new class public class MyThread extends Thread { And somewhere should appear: public void run() { //instructions to be executed in parallel } An this is used … MyThread mt = new MyThread(); Mt.start() NOTAS
Example from Java tutorial public class SimpleThread extends Thread { String nombre; public SimpleThread(String str) { nombre = str; } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i+" "+nombre); try { this.sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} System.out.println("DONE! " + getName()); this.sleep(miliseconds) should appear in a try-catch block NOTAS
Using the thread public class TwoThreadsTest { public static void main (String[] args) { SimpleThread t1,t2; t1 = SimpleThread("Jamaica"); t2 = SimpleThread("Fiji"); t1.start(); t2.start() } start() triggers the parallel execution of run. There are other methods: like join() and interrupt(), suspend(), resume(), stop() are deprecated See ActiveClock and compare it with ActiveClock2 NOTAS
Implementing Runnable public class RunnableExample implements Runnable { public void run () { for (int i = 1; i <= 100; i++) { System.out.println (“Runnable: ” + i); }
Example – an applet that is A Runnable Object The Thread object’s run() method calls the Runnable object’s run() method Allows threads to run inside any object, regardless of inheritance Example – an applet that is also a thread
Starting the Threads public class ThreadsStartExample { public static void main (String argv[]) { //this is previous way new SimpleThread().start (); //this is the new way new Thread(new RunnableExample ()).start (); }