SWE211 Object-Oriented Programming Threads and Sharing Objects Lecture Notes PDF
Document Details
Uploaded by LeadingIntegral
Misr International University
Walaa H. Elashmawi
Tags
Summary
These lecture notes cover object-oriented programming, focusing on threads and sharing objects in Java. The materials include concepts, examples, and diagrams illustrating multithreading. The document uses content from Liang's Java text.
Full Transcript
Object Oriented Programming SWE211 Associate Professor Walaa H. Elashmawi [email protected] Multithreading and sharing objects Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd....
Object Oriented Programming SWE211 Associate Professor Walaa H. Elashmawi [email protected] Multithreading and sharing objects Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 2 Introduction Threads are separate activities within a program, each of which has a beginning and an end. For Ex.: You might want to monitor the keyboard for a key being pressed by a user and, at the same time, track the movement of the mouse by the user and repaint the screen. Each of these tasks can be thought of as a single thread in program. o Threads are not the actual static code itself but rather they are the dynamic process of executing that code. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. For all of the programs that you have written have had only one thread and this has been the thread started by the main method. class ABC { begin …. This can be seen public void main(..) as a single { threaded program body ….. } end } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. A Multithreaded Program Main Thread start start start Thread A Thread B Thread C Threads may switch or exchange data/results Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. Concurrent Systems - Threads Concept o It consists of a collection of separate activities or tasks that are simultaneously at some stage between their starting and finishing point and each task is made into a thread. each thread can Multiple threads on truly run at the multiple CPUs same time as all of the other threads Multiple threads sharing a single the threads share CPU the CPU, and the operating system will allocate small blocks of time to each Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson thread. Education, Ltd. 6 All rights reserved. Main Thread Default Thread in any Java Program JVM uses to execute program statements Ex: Program To Find the Main Thread public class Current { public static void main(String args[]) { Thread t=Thread.currentThread(); System.out.println("Current Thread: "+t); System.out.println("Name is: "+t.getName()); Current Thread: } Thread[main,5,main] Normal priority } Name is: main This means thread name “main” , priority “5” and thread group “main” Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. Creating Threads A thread is treated as an object in Java and the Thread class is found in the java.lang library. –The Thread class has a number of methods for creating, destroying and modifying threads. Threaded classes can be defined in two ways: 1. by inheriting from the Thread class This method is used if you wish to inherit from only one class, the Thread class. Note that Java does not allow multiple inheritance 2. by implementing the Runnable interface. Defining threads by implementing the Runnable interface is used when you want to inherent from a class other than the Thread class. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. Thread class Runnable interface Thread Runnable Thread MyThread MyClass (objects are threads) (objects with run() body) run() method must be overridden (similar to main method of sequential program). run() is called when execution of the thread begins. A thread terminates when run() returns. start() method invokes run(). Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. Inherit from the Thread class The basic steps for creating thread (1)Define a class by extending the Thread class and overriding the run method. class MyThread extends Thread { public void run() { // thread body of execution } } In the run method, you should write the code that you wish to run when this particular thread has started. (2)Create an instance of the above class MyThread thr1 = new MyThread(); (3)Start running the instance using the start method that is defined in Thread. thr1.start(); Create and Execute: new MyThread().start(); Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. An example class MyThread extends Thread { public void run() { System.out.println(" this thread is running... "); } } public class ThreadEx1 { public static void main(String [] args ) { MyThread t = new MyThread(); t.start(); } } this thread is running... Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. class MyThread extends Thread { public int n; MyThread (int number) { n=number; } In thread...: 2 public void run() { for(int i=0;i