Please enable JavaScript.
Coggle requires JavaScript to display documents.
Multithreaded Programming (Thread calss methods (sleep (to sleep current…
Multithreaded Programming
Multi threaded programing are of 2 types
process-based
A process that is running. A scheduler
execute a piece of code
Process based mutitasking is heavy
The inter process communication between processes are also heavy
Process based multitasking is not under the control of java but thread based multitasking is under java's control
They require their on seperate address space
Context switching form one process to another is also costly
thread-based
smallest unit of dispatachable code
eg: A text editor can print and format text at the same time as long as the those two processes are running in two seperate threads
Thread share the same address space
They share the same process
Inter-thread communication is inexpensive
Context switching from one thread to anther is at a lower cost
Multithreaded multitasking is under java's direct control
Thread priorities
Priorities are set by intigers.
High priority thread run first
Rules of context switching
Thread can voluntarily relinquish control
eg: sleeping
In this scenario all the other threads are examined and higher priority theread that is ready to run is given the CPU
A thread can be preempted by a higher-priority thread
This is called preemptive multitasking
A thread has same priority as the thread witch created it
Main thread has the priority 5 by default
Synchronization
Java implementation of
monitor
Monitor is like a small box in which only one thread can run at a time. All the other threads should wait until this thread process is completed.
monitor is a control mechanism first defined by C.A.R Hoare
It is a model of inter-process synchronization
Java does not have
monitor
class. But each object has it's own implict monitor that is automatically entered when one of the objects synchronized methods is called
Messaging in java
Used for communicating between multiple threads
For some other languages you need to relay on the operating system to communicate with other threads. But in java we have cost effective way of communicating with other threads
Thread calss methods
getName
Obtain thread's name
getPriority
setPriority
set the priority of the thread
isAlive
Determine if a thread is still running
join
Wait for a thread to terminate
run
sleep
to sleep current thread for specified time
static void sleep(long milliseconds) throws InterruptedException
static void sleep(long milliseconds, long nanoseconds) throws InterruptedException
This second form is useful only for environments that allow timing periods as short as nanoseconds
Sleep throws
InterruptedException
when another thread interrupt
start
start a thread by calling its run method
setName
The main tread
This is the one that is executed when the program begins
The main thread is important for two reason
It is the t thread from which other child threads will be spawned
Often it must be the last thread to finish execution because it performs various shutdown actions
Main thread default priority is 5