Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
270 Members
Join this group to post and comment.
Soubhik Maji
Soubhik Maji
Computer Science
12 Jan 2019

Java Synchronization

In this we will go through a brief discussion on prerequisites for synchronization topic like process,threads,multithreading

1.What is a process ?

->A program which is in execution is called process . Like your instructions getting CPU time and memory ,all that .

2. A process can have many Threads  . One main thread and other threads . So, what is now thread,  a thread is a light-weight process which is a subset of the process . Thread is managed by user , whereas process is managed by os .You can also call thread as a unit of process .

3.Multithreading -When at the same time multiple threads can be run to complete a program(a process at the hardware level)   . And can make use of the CPU time and other hardware resources if for example mutiple processors are available . We can it multi-threading. If can improve our execution time of the process/program.

4.So how do you Implement thread in java ...

5.One way Is by extending  Thread class (quickly check how inheritance is done is java , I'm skipping that ) . So , make a class extend thread class and put whatever you want to run in a multithreaded way in a method called public void run() . Suppose your class name is Mythread , then to execute -

Thread t1=new Mythread();

t1 start(); //would automatically call the run method and your code will get executed in a multi-threaded way .

6.Now, suppose your class already inherits from some class .(And java doesnt supports inheritance due to diamond problem)

Now, what do you do .. there is a solution..solution is ..what you do is implement  Runnable interface and do the same thing , as it also got run() method and whatever worked there will work here also  ,just what you need to do is below ..

Mythtread t1=new Mythread();//u create instance of your thread

Thread th=new Thread(t1);//pass your class instance to this 

th.start();//You're done

7. 5 states of thread:-



Iii .running



8.You can set priority of the threads .

9.If running your threads in random fashion causes bugs or you want to run your threads in a order to produce proper results what you do is ,Synchronization .

synchronized void fun(){} // allows only one thread to access this function at a time 

synchronized (this){}// synchronized block 

synchronized static void fun(){}


thread8.join();//rest of the threads only gets executed after thread8 finishes its job

thread8.join(2000);//waits for 2 sec for thread8 to execute and then goes for normal execution

threadyield.. Allows other thread to execute and it goes into runnable state ..

10.Deadlock-When threads are waiting for each other and no one gets executed .

11.Methods for inter-thread communication:-

12.wait()//thread waits until until notified by any other thread 

13.notify()// notifies the waiting thread to run again (running)

14.notifyall()//notifies all the waiting threads

15.Try this on your machine .Look up the code and use the mentioned methods and see what happens .Play with it with your own interesting examples

Shruthi Pillai
Shruthi Pillai
Information Technology
4mos ago

Java synchronization means 2 threads cannot execute the method at same time. Synchronized keyword we are using in java. IN java JVM takes care of it. 

       Synchronized method acquires a lock on the whole object. No other thread can use any synchronized method in the whole object while the method is being run by one thread.