Threads in Java. • JVM allows to create multiple Java threads. – note: standard
JVM may run only a single Java process, but any number of threads.
Java Threads May be Created by: – Extending Thread class ... class Worker1
extends Thread. { public void run() {. System.out.println(“I am a Worker Thread”);.
}.
using a distributed control flow programming model like Java RMI or OMG. CORBA, the ... with 'normal' Java programming are the separation between class and interface of a remote object, the ..... method, as for the Constant Pool references of the cla
A thread is a single sequential flow of control in a program.! Java allows multiple
threads to exist simultaneously.! Threads may be executed either on a ...
In this part of the quiz, we would like to collect some information concerning your
..... your answers by providing on the next page the line number and a short ...
1.3 Threads in Java. Java provides a Thread class for defining user threads. One
way to define a thread is to define a class that extends (i.e. inherits from) class ...
is because Java delegates thread synchronization and scheduling to the
underlying ... In this paper, we present Communicating Threads for Java (CTJ).
Java Threads. • The class java.lang.Thread. – Implements the basic threading
abstraction. – Can extend this class to create your own threads. • The interface ...
Apr 27, 2012 ... Java Threads. ○ When a program executes, the JVM starts a thread to run main()
. – This is the “main thread” of execution. • Each thread is an ...
Thread States And Scheduling. ❑ Short Demo. Advanced Topics in Software
Engineering. 2. Thread Creation. There are two ways to create a thread in Java:.
Introduction to Java threads. Presented by developerWorks, your source for great
tutorials ibm.com/developerWorks. Table of Contents. If you're viewing this ...
May 24, 2006 - ISRN INRIA/RR--4139--FR+ENG apport ... W hen the processor is released by a thread, its execution context ...... [6 ] A. H ollub, P r og ra mm i ng J a v a threads i n the rea l wo r l d , Java W orld, 1998, available at : h !£!
Java and Ada are examples of industry-strength languages for ... ing methods on the same object at the same time and .... out (FIFO) queue within priorities, and notify reactivates the thread that has the ..... warrant manual inspection. Missing ...
Unified Modeling Language, Java, Threads, Concurrency, Synchronization, Static .... some other class, because Java does not support multiple inheritance.
Java's Thread Support Is Not Platform Independent . ...... from run to run, but here's a typical output (on a 200MHz P5,
implementation, we measure the performance of a ... the PicoThread, a lightweight, user-level Java thread that can be ..... PARC 1 to test its performance.
within computer applications. Considering ... nicating requirements and analysis/design concepts to software developers, management, end users, and ...
1. Portable and Efficient Distributed Threads for Java. Eli Tilevich and Yannis
Smaragdakis. College of Computing. Georgia Institute of Technology, Atlanta, GA
...
Apr 22, 2003 - distributed programming complicated and error prone. We present a ... parent remote objects [17] to get transparent distributed threads.
of the class will thus be objects simultaneously used by several processes. •
Defining a monitor is done by defining the corresponding class. We will use a
Java ...
of the threads' executions, which makes the bugs nearly impossible to find and ...
Java source and byte code have the same thread and concurrency model,.
CMSC 132: Object-Oriented Programming II. Threads in Java. Department of
Computer Science. University of Maryland, College Park ...
Jul 5, 2006 - Let us consider the following statement: wait("QA and QB or QC"); ... It is possible that no tread is currently interested in an issued event, so the ...
Threads in Java. To put code in a thread, extend the built-in Thread class and
override run: class HelloThread extends Thread { public void run() {.
Threads in Java
To put code in a thread, extend the built-in Thread class and override run: class HelloThread extends Thread { public void run() { System.out.println("hello"); } Copy }
1
Threads in Java To run a thread, instantiate the class and call start (not run!): Thread t1, t2; t1 = new HelloThread(); t2 = new HelloThread(); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException i) { System.exit(1); Copy }
2
Thread-Local Data Use fields in the Thread class for thread-local data: class HelloThread extends Thread { int id; HelloThread(int id) { this.id = id; } public void run() { System.out.println("hello from " + id); } } ... t1 = new HelloThread(0); t2 = new HelloThread(1); ...
Copy
3
Concurrent Modification
Modifying a variable from multiple threads is as wrong in Java as in C: int counter = 0; class CountThread extends Thread { public void run() { counter++; // unpredictable! } Copy }
4
Synchronization Java’s synchronized is similar to Peril-L’s exclusive, but mutual exclusion is based on an object instead of a statement: Integer counter = 0; class CountThread extends Thread { public void run() { synchronized (counter) { // ok counter++; } } } Copy
5
Synchronization Java’s synchronized is similar to Peril-L’s exclusive, but mutual exclusion is based on an object instead of a statement: Object thing = new Object(); int counter = 0; class CountThread extends Thread { public void run() { synchronized (thing) { // ok counter++; } } } Copy
6
Synchronization Java’s synchronized is similar to Peril-L’s exclusive, but mutual exclusion is based on an object instead of a statement: int counter = 0; class CountThread extends Thread { public void run() { synchronized (this) { // wrong! counter++; } } } Copy
7
Synchronization If a method has the synchronized attribute, then each call is implicitly wrapped with synchronized: class Thing { int counter; public synchronized void inc() { counter++; } } ... Thing t = new Thing(); ... sychronized (t) { t.inc() } t.inc(); // equivalent to previous line ... Copy If a method has arguments, however, the argument expressions are not included in the implicit synchronized 8
Synchronization
Some standar classes, such as Vector, have only synchronized methods. Vector counter = new Vector(); LinkedList counter2 = new LinkedList(); class CountThread extends Thread { public void run() { counter.add(this); // ok counter2.add(this); // not ok! } }
Copy
9
Synchronization
Using only synchronized methods does not mean that your code is thread-safe: Vector v = new Vector(); class CountThread extends Thread { public void run() { v.set(0, 1 + (Integer)v.get(0)); // wrong } } Copy
10
Synchronization
Using only synchronized methods does not mean that your code is thread-safe: Vector v = new Vector(); class CountThread extends Thread { public void run() { v.set(0, 1 + (Integer)v.get(0)); // wrong } } Copy
11
Communicating Between Threads Sometimes you need to get a value from one thread to another: class PutThread extends Thread { public void run() { int v = new Random().nextInt(); ... v ...; // send v System.out.println("sent " + v); } } class GetThread extends Thread { public void run() { int v = ...; // receive v System.out.println("got " + v); } } Copy 12
Communicating Between Threads Use synchronized? Integer box; class PutThread extends Thread { ... synchronized (box) { box = v; } ... } class GetThread extends Thread { ... synchronized (box) { v = box; } ... } Copy
Doesn’t ensure put before get! 13-14
Communicating Between Threads Typical newbie “solution”: boolean ready; int box; class PutThread extends Thread { ... box = v; ready = true; ... } class GetThread extends Thread { ... while (!ready) { Thread.sleep(10); } v = box; ... } Copy 15
Communicating Between Threads Typical newbie “solution”: boolean ready; int box; class PutThread extends Thread { ... box = v; ready = true; ... } GetThread extends Thread { notclass sync'ed ... while (!ready) { Thread.sleep(10); } v = box; ... }
Copy
16
Communicating Between Threads Typical newbie “solution”: boolean ready; int box; class PutThread extends Thread { ... box = v; ready = true; ... }
Communicating Between Threads Java includes lots of data structures to solve these kinds of problems: SynchronousQueue q; class PutThread extends Thread { ... q.add(v); ... } class GetThread extends Thread { ... v = (Integer)q.take(); ... } Copy 18
Communicating Between Threads From scratch, analogous to POSIX support: boolean ready; int box; Lock lock = new ReentrantLock(); Condition nowReady = lock.newCondition(); class PutThread extends Thread { ... lock.lock(); box = v; ready = true; nowReady.signal(); lock.unlock(); ... }
Copy
19
Communicating Between Threads
From scratch, continued: class GetThread extends Thread { ... lock.lock(); while (!ready) { nowReady.await(); } v = box; lock.unlock(); ... Copy }