Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Multithreading: Multi-Programming, Thread Classes, and Synchronization, Summaries of Software Engineering

The concept of multi-threading in java, focusing on multi-programming, thread classes, and synchronization. It covers the difference between multi-user, multi-tasking, and multi-threading, the use of thread classes and runnable interfaces, and the importance of thread priorities and states. Additionally, it discusses the concept of critical sections, mutual exclusion, locking, semaphores, monitors, and inter-thread communication.

Typology: Summaries

2022/2023

Uploaded on 01/26/2024

mtiku-tadesse
mtiku-tadesse 🇪🇹

4 documents

1 / 16

Toggle sidebar

Related documents


Partial preview of the text

Download Java Multithreading: Multi-Programming, Thread Classes, and Synchronization and more Summaries Software Engineering in PDF only on Docsity! MULTI-THREADING WHAT IS MULTI_PROGRAMMING: ➢ Running more than one program that is running multiple programs on a single machine or a computer is known as multi-programming. ➢ The idea of multiprogramming started from the utilisation of the CPU when it is idle as the CPU works for just few time in the whole hour. ➢ There are different form of multi-programming. ➔ Multi-user: more than one user using the machine / running their programs simultaneously. • for connecting more than one user to single computer the DUMMY TERMINALS were used. • here the ROUND ROBIN fashion was introduced as the programs were executed simultaneously. • UNIX and LINUX are famous operating systems for the multi- user environment. • Multi-user machines were known as TIME_SHARING machines. ➔ Multi-Tasking: single user runs multiple tasks simultaneously. • here the CPU runs the programs alternatively on high rate. • WINDOWS and MacOS(OS X) operating system supports this type of environment. ➔ MULTI_THREADING: it is a type of multi-threading where there are different tasks going on under a single application. • threads are light weighted compared to the task. • CPU runs the threads alternatively where the user fells the threads running all together. • examples: animation, application, gaming, websites, webserver CONTROL FLOW OF A PROGRAM ➢ A single program in java contains one control Flow. ➢ Entry point of the program is main method which executes at First. ➢ Example program: ➢ The second method which executes is the calling method. ➢ The third method which Is executed is the called method. ➢ The above program is example explaining that the main method is the only thread which controls the Flow of the program. ➢ It is just like getting a reference of a page while reading a book. ➢ If there is an inFinite loop in the display method then the Flow of program does not executes further so there needs to be two Flow of controls to execute the program. MULTITHREADING USING THREAD CLASSES ➢ Java provides thread class and runnable interface to achieve multithreading. ➢ Thread class contains the actual mechanism for multithreading. ➢ In java a class can extend from only one class. class Test { static void display( ) { s.o.p(“HELLO”); } p.s.v main(…) { display( ); s.o.p(“WORLD”); } } ➢ Then it enters into the running state. ➢ After completing the task it will enter into the terminated state. ➢ A thread which is terminated is just like a thread which is killed. ➢ Therefore the different states of thread are NEW ! READY ! RUNNING ! TERMINATED ➢ While running the thread may also enter into different states like: • WAIT STATE: waiting for acquiring some resource or made to wait by some other thread. • TIME WAIT STATE: to make the thread to delay for some time using the sleep method, it is also known as sleep state • WAIT AND NOTIFY: where the thread is to be in the waiting state to get to its chance till it gets notiFied. • BLOCKED STATE: it is just like entering into the monitor where the thread is being locked for some time, it is similar to waiting state. THREAD PRIORITIES ➢ JAVA supports thread priorities from 1-10. ➢ Execution of threads depends upon scheduler. ➢ If a thread is having higher priority then it should get some preference over other threads. ➢ In java there are different levels of priority that are: • MIN_PRIORITY=1. • NORM_PRIORITY=5. • MAX_PRIORITY=10. ➢ The priority of default thread is always 5. ➢ The higher priority is given to the thread which gets the input or the data. ➢ The thread with higher priority gets the more CPU time. ➢ Multithreading features are provided by the operating systems but in java JVM have its own scheduler. THREAD CLASS ➢ Object of the thread class can be created. ➢ Whenever a thread is created it gets some IDE. ➢ Threads can be identiFied by mentioning their names. ➢ There are different constructors to give the thread classes: ➔ Thread( ) • it is a default class. ➔ Thread(Runnable r) • the thread contains the runnable interface. ➔ Thread(Runnable r, String name) • the thread class have its own name with runnable interface. ➔ Thread(ThreadGroup g, String name) • thread group to manage various threads together. for example: In MS Word one thread takes the input from the keyboard, another thread checks for the spellings which works simultaneously, another thread which works to auto save the document. In the above the First priority is given to the thread which takes the input ➔ Thread(String name) • the thread class have its own name. ➢ There are various getter and setter methods: ➔ long getId( ) • to get the id of particular thread. ➔ String getName( ) • to get the name of the thread mentioned. ➔ int getPriority( ) • to know the current priority of the thread. ➔ Thread.state getState( ) • to get to know the state of the thread . ➔ ThreadGroup getthreadGroup( ) • to know the group to which the thread belongs. ➔ void setName(String name) • to set the Name of the thread class. ➔ void setPriority(int p) • to set the priority of the thread class. ➔ void setDaemon(Boolean d) • to set a background thread with least priority with no user interaction. For example: Different types of balloons or balls in an animation having their own thread classes is the example for the thread group. For example: The garbage collector in the JVM which have the least priority. • the mutual exclusion states that the accessing of one thread prevents the accessing of another. ➔ Locking/Mutex: • Mutex is the variable used for locking the threads. • if the mutex variable is set to zero then it is free or it is not occupied. • when the time period of one thread is Finished then the another thread cannot access the object as the mutex will not remain zero. • thread two can access the object if and only if the mutex is zero again. • for every shared resource there should be a lock which is being applied by the thread itself. • here the threads are responsible for mutual exclusion. • mutex was not useful as the threads would be overlapping each other if the mutex was not being locked by the First one. ➔ Semaphore: • semaphore was like an operating system before the introducing of java to control the coordination of threads as they should not overlap. • it was supported by UNIX operating system. • the semaphore creates the scenario where the thread which have been occupying the object would signal the other after its work is Finished. • it creates a block queue where the upcoming thread is to be in waiting state. • the methods used here are wait( ) and signal( ). • here the operating system have the mutual exclusion. ➔ Monitor: • here the object itself takes the responsibility for the mutual exclusion. • it can be achieved using object orientation. • the complete mechanism is inside the object itself. • the read and write method, the data and the block queue belongs to the shared object itself as it can be accessed by any of the threads at a particular time. • Here java makes sure than one thread is accessed at a time. • example program: class MyData { void display (String str) { synchronize(this) { for(int i=0;i<str.length();i++) { s.o.p(str.charAt(i)); } } } } class MyThread1 extends Thread { Mydata d; MyThread1(MyData dat){d=dat;} public void run() { d.display(“Hello World”); } } class MyThread2 extends Thread { Mydata data; MyThread2(MyData dat) {data=dat;} public void run() { d.display(“Welcome”); } } class Test { p.s.v.main(…) { MyData d=new Mydata( ); Mythread1 t1=new MyThread1(d); MyThread2 t2=new MyThread(d); t1.start( ); t2.start( ); class MyData { int value=0; boolean flag=true; synchronised void set(int v) { while(flag!=true) wait( ); value=v; flag=false; notify( ); } synchronized int get( ) { int x=0; while(flag!=false) wait( ) X=value; flag=true; notify; return x; } } class Producer extends Thread { MyData d; Producer(Mydata dat){d=dat;} public void run( ) { Int i=1; while(ture) { d.set(i); s.o.p(“producer: “+i); i++; } } } class consumer extends thread { MyData d; In the above example: • the inter-thread communication part is achieve by the programmer itself. • the producer will be writing in the shared object and the consumer will be reading from the shared object. • the shared object, have three things one is the data or the value given, second is the set method to write the data and the third is the get method to read the data. • as the set and get methods are synchronised only one thread is allowed to enter. • If the Flag is true then it is producers turn and if the Flag is false then it is the consumers turn • wait( ) and notify( ) is to bring in the communication between the threads.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved