Packages Packages are containers for classes that are used to keep the class name space compartmentalized.. You can define classes inside a package that.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Advertisements

Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Definitions Process – An executing program
Lecture 4 : JAVA Thread Programming
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Java Programming: Advanced Topics
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
10/10/20151 MULTITHREADED PROGRAMMING. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called.
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.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
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.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Threads.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and 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
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Java Thread and Memory Model
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.
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.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Multithreading and Garbage Collection Session 16.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
6/11/2016 DEPT OF CSE 1 JAVA. 6/11/2016 DEPT OF CSE 2 MULTITHREADED PROGRAMMING.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Multithreading / Concurrency
Chapter 13: Multithreading
Multi Threading.
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
Multithreaded Programming in Java
Multithreading in Java
Multithreading programming Pavan d.m.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Exception Handling Visit for more Learning Resources.
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Chapter 15 Multithreading
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Packages Packages are containers for classes that are used to keep the class name space compartmentalized.. You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are only exposed to other members of the same package.

Defining a Package To create a package simply include a package command as the first statement in a Java source file. The package statement defines a name space in which classes are stored. If you omit the package statement, the class names are put into the default package, which has no name. the general form of the package statement: package pkg;

Ex:package p1; the.class files for any classes you declare to be part of p1 must be stored in a directory called p1.

package p1; class Balance { String name; double bal; Balance(String n, double b) { name = n; bal = b; } void show() { if(bal<0) System.out.println(name + ": $" + bal); } class AccountBalance { public static void main(String args[]) { Balance current[] = new Balance[3]; current[0] = new Balance("K. J. Fielding", ); current[1] = new Balance("Will Tell", ); current[2] = new Balance("Tom Jackson", ); for(int i=0; i<3; i++) current[i].show(); }

Threads A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking. there are two distinct types of multitasking: process-based and thread-based.

ProcessBased MultiTasking A process is, a program that is executing. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently. Ex:run the Java compiler at the same time that you are using a text editor. thread-based multitasking In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code. This means that a single program can perform two or more tasks simultaneously. Ex: a text editor can format text at the same time that it is printing,

Threads exist in several states. A thread can be running. It can be ready to run as soon as it gets CPU time. A running thread can be suspended, which temporarily suspends its activity. A suspended thread can then be resumed, allowing it to pick up where it left off. A thread can be blocked when waiting for a resource. At any time, a thread can be terminated, which halts its execution immediately. Once terminated, a thread cannot be resumed.

Runnable Runni ng Termi nated Non Runn able New

The Main Thread When a Java program starts up, one thread begins running immediately. This is called the main thread of your program, because it is the one that is executed when your program begins. It is the thread from which other "child" threads will be spawned. It must be the last thread to finish execution. When the main thread stops, your program terminates. the main thread can be controlled through a Thread object. To do so, you must obtain a reference to it by calling the method currentThread( ), which is a public static member of Thread.

class ThreadDemo { public static void main(String args[]) {Thread t = Thread.currentThread(); System.out.println("Current thread: " + t); t.setName("My Thread"); System.out.println("After name change: " + t); System.out.println("After name change: " + t.getName()); for(int n = 5; n > 0;n--) { System.out.println(n); try { Thread.sleep(1000); }catch(InterruptedException e){} }

static Thread currentThread( ) Returns the currentThread static void sleep(long milliseconds) throws InterruptedException Suspend the thraed for milliseconds. static void sleep(long milliseconds, int nanoseconds) throws InterruptedException final void setName(String threadName) Sets the Name of the Thread final String getName( ) Returns the name of the Thread. Note : InterruptedException is a Checked Exception

Creating a Thread Java defines two ways in which this can be accomplished: By implementing the Runnable interface. By extending the Thread class, itself. Implementing Runnable create a class that implements the Runnable interface. You can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run( ), which is declared like public void run( ) run( ) establishes the entry point for another, concurrent thread of execution within your program. This thread will end when run( ) returns.

class thread implements Runnable { Thread t; n thread() { t=new Thread(this,"Nisha"); t.start(); } public void run() { for(int i=0;i<5;i++) { System.out.println("nish"+i); try { Thread.sleep(1000); }catch(InterruptedException e) { } } class ThreadDemo { public static void main(String args[]) { Thread t = Thread.currentThread(); t.setName("My Thread"); thread T =new thread(); for(int n = 5; n > 0;n--) { System.out.println(n); try { Thread.sleep(1500); }catch(InterruptedException e){} }

The methods in Thread Class Constructor Thread(Runnable threadOb, String threadName) threadOb is an instance of a class that implements the Runnable interface. The name of the new thread is specified by threadName. void start( ) start( ) executes a call to run() and the thread started running

Extending Thread The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread.

class Threadclass extends Thread { Threadclass() { super("Nisha"); start(); System.out.println(getName()); } public void run() {for(int i=5;i>0;i--) {System.out.println(getName()+i); try { sleep(1000); }catch(InterruptedException e){} }

class ThreadDemo { public static void main(String a[]) { new Threadclass(); System.out.println(Thread.currentThread().getName()); for(int i=0;i<=5;i++) { System.out.println("Main"+i); try { Thread.sleep(1500); }catch(InterruptedException e) {} }

Choosing an Approach it is probably best to implement Runnable. Thread class is having lot of methods in which only the run method is overriding.

class Threadclass implements Runnable {Thread t; String s; Threadclass(String s1) { s=s1; t=new Thread(this,s); System.out.println(t.getName()); t.start(); } public void run() {for(int i=5;i>0;i--) {System.out.println(s+i); try {Thread.sleep(100); }catch(InterruptedException e){} } System.out.println("Finished"+s); }

class ThreadDemo {public static void main(String a[]) {new Threadclass("First"); new Threadclass("Second"); new Threadclass("Third"); System.out.println(Thread.currentThread().getName()); for(int i=0;i<=5;i++) {System.out.println("Main"+i); try { Thread.sleep(2000); }catch(InterruptedException e) {} }System.out.println("FinishedMain"); }

final boolean isAlive( ) The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise. final void join( ) throws InterruptedException This method waits until the thread on which it is called terminates.

class Threadclass implements Runnable {Thread t; String s; Threadclass(String s1) { s=s1; t=new Thread(this,s); System.out.println(t.getName()); t.start(); } public void run() {for(int i=5;i>0;i--) {try{ System.out.println(s+i); Thread.sleep(100); }catch(InterruptedException e) {} } System.out.println("Finished"+s); }}

class ThreadDemo {public static void main(String a[]) {Threadclass Tc1,Tc2,Tc3 ; Tc1=new Threadclass("First"); Tc2=new Threadclass("Second"); Tc3=new Threadclass("Third"); System.out.println(Thread.currentThread().getName()); System.out.println(Thread.currentThread().isAlive()); System.out.println(Tc1.t.isAlive()); System.out.println(Tc2.t.isAlive()); System.out.println(Tc3.t.isAlive()); try{Tc1.t.join(); System.out.println(Tc1.t.isAlive()); Tc2.t.join(); System.out.println(Tc2.t.isAlive()); Tc3.t.join(); }catch(Exception e){ } System.out.println(Tc3.t.isAlive()); System.out.println("Finished Main"); }}

Thread Priorities In theory, higher-priority threads get more CPU time than lower-priority threads. In a preemptive environment A higher-priority thread can preempt a lower-priority one.when a lower-priority thread is running and a higher-priority thread resumes (from sleeping or waiting on I/O, for example), it will preempt the lower-priority thread. To set a thread's priority, use the setPriority( ) method, which is a member of Thread.

final void setPriority(int level) The value of level must be within the range MIN_PRIORITY and MAX_PRIORITY. Currently, these values are 1 and 10, respectively. To return a thread to default priority, specify NORM_PRIORITY, which is currently 5. These priorities are defined as final variables within Thread. final int getPriority( ) To obtain the current priority

class ThreadClass implements Runnable {Thread t; private volatile boolean ok= true; int count; ThreadClass(int p,String s) {t=new Thread(this,s); t.setPriority(p); count=0; ok=true; } public void run() {while(ok) { count++; } } void start() { t.start(); } void stop() { ok=false;} }

class ThreadDemo {public static void main(String a[]) { ThreadClass t1=new ThreadClass(Thread.NORM_PRIORITY-2,"FIRST"); ThreadClass t2=new ThreadClass(Thread.NORM_PRIORITY+2,"Secd"); t1.start(); t2.start(); for(int i=0;i<5;i++) {System.out.println("main"+i); try{Thread.sleep(500); }catch(InterruptedException e){ } } t1.stop(); t2.stop(); try{t1.t.join(); t2.t.join(); }catch(InterruptedException e){ } System.out.println(t1.count); System.out.println(t2.count); }}

The higher-priority thread got approximately 90 percent of the CPU time. the use of volatile, prevents Java from optimizing the code of the loop in such a way that the value of running is held in a register of the CPU and not necessarily reexamined with each iteration. Synchronization When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization. Key to synchronization is the concept of the monitor (also called a semaphore). A monitor is an object that is used as a mutually exclusive lo ck, or mutex. Only one thread can own a monitor at a given time. To enter an object's monitor, just call a method that has been modified with the synchronized keyword. While a thread is inside a synchronized method, all other threads that try to call it on the same instance have to wait.

class Callme {synchronized void call(String msg) { System.out.print("[" + msg); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); }

class Caller implements Runnable {String msg; Callme target; Thread t; public Caller(Callme targ, String s) {target = targ; msg = s; t = new Thread(this); t.start(); } public void run() {target.call(msg); }}

class Synch { public static void main(String args[]) {Callme target = new Callme(); Caller ob1 = new Caller(target, "Hello"); Caller ob2 = new Caller(target, "Synchronized"); Caller ob3 = new Caller(target, "World"); try { ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); }}} o/p [Hello] [Synchronized] [World]

How can access to an object of a class be synchronized? put calls to the methods defined by this class inside a synchronized block. This is the general form of the synchronized statement: synchronized(object) { // statements to be synchronized } Here, object is a reference to the object being synchronized.

class Callme { void call(String msg) { System.out.print("[" + msg); try { Thread.sleep(1000); } catch (InterruptedException e) {System.out.println("Interrupted"); } System.out.println("]"); }

class Caller implements Runnable {String msg; Callme target; Thread t; public Caller(Callme targ, String s) {target = targ; msg = s; t = new Thread(this); t.start(); } public void run() {synchronized(target) { target.call(msg); } }}

class Synch1 {public static void main(String args[]) {Callme target = new Callme(); Caller ob1 = new Caller(target, "Hello"); Caller ob2 = new Caller(target, "Synchronized"); Caller ob3 = new Caller(target, "World"); try { ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch(InterruptedException e) {}; }

Interthread Communication interface ProducerThread ConsumerThread consider the classic queuing problem, where one thread is producing some data and another is consuming it the producer has to wait until the consumer is finished before it generates more data.But In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling,wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable. To avoid this, Java includes an elegant interprocesscommunication mechanism using the wait( ), notify( ), and notifyAll( ) methods. These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized method.

final void wait( ) throws InterruptedException tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ). final void notify( ) wakes up the first thread that called wait( ) on the same object. final void notifyAll( ) wakes up all the threads that called wait( ) on the same object. Thehighest priority thread will run first. These methods are declared within Object class

class Q {int n; boolean flag=false; synchronized void get() {if (!flag) try{ wait();}catch(Exception e){} System.out.println("Got: " + n); flag=false; notify(); } synchronized void put(int n) {if (flag) try{ wait(); }catch(Exception e){} this.n = n; System.out.println("Put: " + n); flag=true; notify(); }}

class Producer implements Runnable {Q q; Producer(Q q) {this.q = q; new Thread(this, "Producer").start(); } public void run() {int i = 0; while(true) {q.put(i++);} } class Consumer implements Runnable {Q q; Consumer(Q q) {this.q = q; new Thread(this, "Consumer").start(); } public void run() {while(true) {q.get();} }}

class PC { public static void main(String args[]) {Q q = new Q(); new Producer(q); new Consumer(q); System.out.println("Press Control-C to stop."); }

class Q {int n; synchronized void get() {System.out.println("Got: " + n); } synchronized void put(int n) {this.n = n; System.out.println("Put: " + n); }}

Deadlock Deadlock occurs when two threads have a circular dependency on a pair of synchronized objects. For example, suppose one thread enters the monitor on object X and another thread enters the monitor on object Y. If the thread in X tries to call any synchronized method on Y, it will block as expected. However, if the thread in Y, in turn,tries to call any synchronized method on X, the thread waits forever, because to access X, it would have to release its own lock on Y so that the first thread could complete.

class A { synchronized void display(B b) {System.out.println("Entered A display"); try{ Thread.sleep(1000);}catch(InterruptedException e){} b.display1(); } synchronized void display1() { System.out.println("Entered A Display1"); }

class B implements Runnable {Thread t;A a1; B(A a) {t=new Thread(this);a1=a; t.start(); display(a1); } public void run() {a1.display(this);} synchronized void display(A a) {System.out.println("Entered B Display"); try{Thread.sleep(1000);}catch(InterruptedException e){} a.display1(); } synchronized void display1() {System.out.println("Entered Display1");} public static void main(String a[]) {A a1=new A(); B b1=new B(a1); }}