Skip to main content

Java Multithreaded Programming

A program that has two or parts can run concurrently. Each part is a thread. So this is a specialised form multitasking.

Process based multitasking is heavyweight and use seperate address spaces. It gives the big picture of the application. JVM has control as it handled by os level. Thread based is light weight and use same address space. So inter communication between threads is easy. This gives detail view of the part it's running. JVM has full control.

Multithreading reduces idle time by running another when one is waiting for something like slow network operation.
And this avoids application being blocked for something.

In a single core CPU no actual parallel threading is happening but CPU time is allocated between threads.

Running thread can be suspended and resumed. Can be blocked when waiting. When terminated can't resume.

Threads has priorities. Higher priority threads can take cpu power by pushing low or low priority threads can volunteeraly allow if its blocked. For same priority threads windows will slice cpu time but on other os will till other finishers.

Once a thread is in a synchronized method, no other threads can access it. Java thread to thread communication can be done via synchronized methods.

Java multithreading is based on thread class and it's runnable interface. Dealing with thread is done using its proxy. Creation is done by extending thread class or implementing runnable interface.

Thread join will wait till a thread to terminate. Run is the entry point. Sleep suspend a thread for some time. Start calls run method. 

You can obtain a reference to a thread by calling static Thread.currentThread () method.

Creating Threads

A thread can be created by implementing runnable interface or by extending thread class.

To implement runnable only method requires to implement in Runnable is run()  it has all code to execute in new thread. Need to create a thread object within this class and call thread.start() which makes a calll to run() method.

Extending thread class. Must override run method and call start to begin new thread.

Runnable implementation allows multiple inheritance while extending helps more control.

IsAlive helps knowing status of a another thread. Join() can use to a wait till another thread finishes.

Thread priority decides CPU time for the thread. When priority is equal thread behaviour could be different based on the environment.

Synchronization
All threads trying to access same resource at same time is called race condition, all threads racing each other to complete the method. Synchronization Allows only one thread at a time to access a shared resource.
It has a lock called monitor to lock the resources. All objects have access to there monitors.
When one thread is entered into synchronized method other synchronized method in the same instance is also locked for the same thread.

Has two ways to use synchronize.
1. Synchronized void method () {}
Updating the methods
2. Synchronized (object) {}
Calling method within synchronized. Can use to call 3rd party classes.

Inter thread communication
is done using wait and notify method. Wait () will put the calling thread to sleep and leave the monitor until some other thread use the same monitor and call notify () or notify all()
Notify() wakes up the thread that called wait on the same object.
Wait should be called within a loop to check wait conditions as some times thread can start for no reason.

Deadlock
is possible when two thread try to access single resources concurrently

Thread control
Suspend resume and stop Is not allowed from Java version 2. This courses system errors. So it should be done using flags with wait and notify methods

Thread state
GetState() returns the state of the thread.
Blocked- waiting to acquire a lock
New - not begun execution
Runnable- either running or can run when CPU is provided
Terminated- execution completed
Timed waiting- when thread is in sleep and time out versions of sleep and join states
Waiting- non timeout versions of wait and join

Comments

Popular posts from this blog

Oracle Database 12c installation on Ubuntu 16.04

This article describes how to install Oracle 12c 64bit database on Ubuntu 16.04 64bit. Download software  Download the Oracle software from OTN or MOS or get a downloaded zip file. OTN: Oracle Database 12c Release 1 (12.1.0.2) Software (64-bit). edelivery: Oracle Database 12c Release 1 (12.1.0.2) Software (64-bit)   Unpacking  You should have following two files downloaded now. linuxamd64_12102_database_1of2.zip linuxamd64_12102_database_2of2.zip Unzip and copy them to \tmp\databases NOTE: you might have to merge two unzipped folders to create a single folder. Create new groups and users Open a terminal and execute following commands. you might need root permission. groupadd -g 502 oinstall groupadd -g 503 dba groupadd -g 504 oper groupadd -g 505 asmadmin Now create the oracle user useradd -u 502 -g oinstall -G dba,asmadmin,oper -s /bin/bash -m oracle You will prompt to set to password. set a momorable password and write it down. (mine is orac

DBCA : No Protocol specified

when trying to execute dbca from linux terminal got this error message. now execute the command xhost, you probably receiving No protocol specified xhost:  unable to open display ":0" issue is your user is not allowed to access the x server. You can use xhost to limit access for X server for security reasons. probably you are logged in as oracle user. switch back to default user and execute xhost again. you should see something like SI:localuser:nuwan solution is adding the oracle to access control list xhost +SI:localuser:oracle now go back to oracle user and try dbca it should be working

Java Head Dump Vs Thread Dump

JVM head dump is a snapshot of a JVM heap memory in a given time. So its simply a heap representation of JVM. That is the state of the objects. JVM thread dump is a snapshot of a JVM threads at a given time. So thats what were threads doing at any given time. This is the state of threads. This helps understanding such as locked threads, hanged threads and running threads. Head dump has more information of java class level information than a thread dump. For example Head dump is good to analyse JVM heap memory issues and OutOfMemoryError errors. JVM head dump is generated automatically when there is something like OutOfMemoryError has taken place.  Heap dump can be created manually by killing the process using kill -3 . Generating a heap dump is a intensive computing task, which will probably hang your jvm. so itsn't a methond to use offetenly. Heap can be analysed using tools such as eclipse memory analyser. Core dump is a os level memory usage of objects. It has more informaiton t