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

This page cannot be seen from the preview

Don't miss anything!

bg1
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&&
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

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”); } }

➢ (^) Example program using runnable interface: Interfaces are implemented. ➢ The class becomes abstract if it does not implements all the features of interface. ➢ (^) In the above program it gives the example that the object also runs when the thread runs.

STATES OF A THREAD

➢ The First state of the thread is new it stores the object of the thread. ➢ (^) To run the object of thread the start method is called. ➢ (^) When start method is called then it is entered into the ready state where it is ready to run. class My implements Runnable { public void run( ); { Int i= while(true) { s.o.p(i+“hello”); i++; } } } class Test { p.s.v main( ) { My m=new My( ); Thread t=new Thread(m); t.start( ); int i=1; while(true) { s.o.p(i+”world”); i++; } } }

➢ (^) 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.

➔ (^) 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.

➢ There are different methods for the enquirying the thread classes: ➔ (^) boolean isAlive( )

  • (^) to check if the thread is alive or terminated. ➔ (^) boolean isDaemon( )
  • (^) to check if the thread is acting as a daemon or not. ➔ (^) boolean isInterrupted( )
  • (^) to check whether the thread is interrupted by some other methods or not. ➢ (^) There are different instance methods for the thread classes: ➔ (^) void interrupt( )
  • (^) it is used to interrupt the thread mostly in the state of waiting or sleeping. ➔ (^) void join( )
  • (^) instead of terminating the thread is been kept waiting to join with other thread until it is completed using the join method. ➔ (^) void join(long millis)
  • (^) it is used to join the main function with other for a period of time. ➔ (^) void run( )
  • (^) it has the actual functionality of thread and it can be overided ➔ (^) void start( ) ➢ (^) There are different static methods for the thread classes: ➢ (^) These can be called by just using the class name. ➔ (^) Int activeCount( )
  • (^) it gives the number of threads active in a particular group.
  • (^) 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:
  • (^) the classes Mythread1 and Mythread2 access the data from the display class which is the shared object.
  • (^) the synchronize method is used to execute the classes separately in the above example.
  • (^) synchronize method prints the data separately from both thread in other words it prevents the data to be overlapped.
  • (^) the for loop inside the synchronize prints one method at a time.
  • (^) The other method for synchronization is to write the synchronize at the signature method of the display class so the whole method is being synchronized. ➔ (^) Inter-thread communication:
  • (^) it is the communication between the synchronized threads.
  • (^) it is the communication between a single producer thread and a consumer thread.
  • (^) the inter-thread communication refers to the synchronization between the producer thread and the consumer thread to access the write and read method simultaneously.
  • (^) to achieve the communication the Flag=t and Flag=f, are used. ➔ (^) Race condition:
  • (^) here there is a single producer thread and multiple consumer threads.
  • (^) here all consumers do not execute at once they do in a round robin fashion.
  • (^) when the count is zero then it is the producers turn.
  • (^) and when the count is not zero then it is the consumers turn.
  • (^) since there are more than one consumer any of the consumer can access it.
  • (^) the notify method can open any thread her as they may not be in an order.
  • (^) The race condition states that
  • (^) If one thread is accessing the shared resource and all other are blocked then once the thread has Finished.
  • (^) working it will inform the thread and any of the threads may access the object just like in a race.
  • (^) So, in the above the count method is used to control the race.
  • (^) the race condition can be avoided by the inter-thread condition.
  • (^) example program:

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.