1669171899
Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.
Why we use Synchronization
Synchronization is classified into two types
Thread synchronization is two types, they are:
1.Mutual Exclusive:
A Mutex or Mutual Exclusive helps only one thread to access the shared resources. It won’t allow the accessing of shared resources at a time. It can be achieved in the following ways.
2. Cooperation (Inter Thread Communication in java)
Also check Java Tutorial for Beginners | An Overview of Java
Below example shows the Powers of the numbers like n1, n2, n3, n4, n5
class Power{
void printPower(int n){//method not synchronized
int temp = 1;
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName() + ":- " +n + "^"+ i + " value: " + n*temp);
temp = n*temp;
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
}
}
}
class Thread1 extends Thread{
Power p;
Thread1(Power p){
this.p=p;
}
public void run(){
p.printPower(5);
}
}
class Thread2 extends Thread{
Power p;
Thread2(Power p){
this.p=p;
}
public void run(){
p.printPower(8);
}
}
public class Synchronization_Example1{
public static void main(String args[]){
Power obj = new Power();//only one object
Thread1 p1=new Thread1(obj);
Thread2 p2=new Thread2(obj);
p1.start();
p2.start();
}
}
Output:
Thread-1:- 8^1 value: 8
Thread-0:- 5^1 value: 5
Thread-1:- 8^2 value: 64
Thread-0:- 5^2 value: 25
Thread-1:- 8^3 value: 512
Thread-0:- 5^3 value: 125
Thread-1:- 8^4 value: 4096
Thread-0:- 5^4 value: 625
Thread-1:- 8^5 value: 32768
Thread-0:- 5^5 value: 3125
Here we didn’t use the synchronized keyword so both the threads are executing at a time so in the output, thread-0 is interfering with thread-1, and hence, we are getting inconsistent results.
If we use the Synchronized keywords in any method then that method is Synchronized Method.
Syntax:
Acess_modifiers synchronized return_type method_name (Method_Parameters) {
// Code of the Method.
}
class Power{
synchronized void printPower(int n){//method synchronized
int temp = 1;
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName() + ":- " +n + "^"+ i + " value: " + n*temp);
temp = n*temp;
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
}
}
}
class Thread1 extends Thread{
Power p;
Thread1(Power p){
this.p=p;
}
public void run(){
p.printPower(5);
}
}
class Thread2 extends Thread{
Power p;
Thread2(Power p){
this.p=p;
}
public void run(){
p.printPower(8);
}
}
public class Synchronization_Example2{
public static void main(String args[]){
Power obj = new Power();//only one object
Thread1 p1=new Thread1(obj);
Thread2 p2=new Thread2(obj);
p1.start();
p2.start();
}
}
Output:
Thread-0:- 5^1 value: 5
Thread-0:- 5^2 value: 25
Thread-0:- 5^3 value: 125
Thread-0:- 5^4 value: 625
Thread-0:- 5^5 value: 3125
Thread-1:- 8^1 value: 8
Thread-1: – 8^2 value: 64
Thread-1:- 8^3 value: 512
Thread-1:- 8^4 value: 4096
Thread-1:- 8^5 value: 32768
Here we used synchronized keywords. It helps to execute a single thread at a time. It is not allowing another thread to execute until the first one is completed, after completion of the first thread it allowed the second thread. Now we can see the output correctly the powers 5 and 8 from n1 to n5. Thread-0 completed then only thread-1 begin.
Syntax:
synchronized (object) {
//code of the block.
}
Program to understand the Synchronized Block:
class Power{
void printPower(int n){
synchronized(this){ //synchronized block
int temp = 1;
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName() + ":- " +n + "^"+ i + " value: " + n*temp);
temp = n*temp;
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
}
}
}
}
class Thread1 extends Thread{
Power p;
Thread1(Power p){
this.p=p;
}
public void run(){
p.printPower(5);
}
}
class Thread2 extends Thread{
Power p;
Thread2(Power p){
this.p=p;
}
public void run(){
p.printPower(8);
}
}
public class Synchronization_Example3{
public static void main(String args[]){
Power obj = new Power();//only one object
Thread1 p1=new Thread1(obj);
Thread2 p2=new Thread2(obj);
p1.start();
p2.start();
}
}
Output:
Thread-0:- 5^1 value: 5
Thread-0:- 5^2 value: 25
Thread-0:- 5^3 value: 125
Thread-0:- 5^4 value: 625
Thread-0:- 5^5 value: 3125
Thread-1:- 8^1 value: 8
Thread-1:- 8^2 value: 64
Thread-1:- 8^3 value: 512
Thread-1:- 8^4 value: 4096
Thread-1:- 8^5 value: 32768
In this example, we didn’t synchronize the entire method but we synchronized few lines of code in the method. We got the results exactly as the synchronized method.
Syntax:
synchronized static return_type method_name (Parameters) {
//code
}
Or
synchronized static return_type method_name (Class_name.class) {
//code
}
Program without Static Synchronization:
class Power{
synchronized void printPower(int n){ //static synchronized method
int temp = 1;
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName() + ":- " +n + "^"+ i + " value: " + n*temp);
temp = n*temp;
try{
Thread.sleep(400);
}catch(Exception e){}
}
}
}
class Thread1 extends Thread{
Power p;
Thread1(Power p){
this.p=p;
}
public void run(){
p.printPower(2);
}
}
class Thread2 extends Thread{
Power p;
Thread2(Power p){
this.p=p;
}
public void run(){
p.printPower(3);
}
}
class Thread3 extends Thread{
Power p;
Thread3(Power p){
this.p=p;
}
public void run(){
p.printPower(5);
}
}
class Thread4 extends Thread{
Power p;
Thread4(Power p){
this.p=p;
}
public void run(){
p.printPower(8);
}
}
public class Synchronization_Example4{
public static void main(String args[]){
Power ob1 = new Power(); //first object
Power ob2 = new Power(); //second object
Thread1 p1 = new Thread1(ob1);
Thread2 p2 = new Thread2(ob1);
Thread3 p3 = new Thread3(ob2);
Thread4 p4 = new Thread4(ob2);
p1.start();
p2.start();
p3.start();
p4.start();
}
}
Output:
Thread-2:- 5^1 value: 5
Thread-0:- 2^1 value: 2
Thread-2:- 5^2 value: 25
Thread-0:- 2^2 value: 4
Thread-2:- 5^3 value: 125
Thread-0:- 2^3 value: 8
Thread-2:- 5^4 value: 625
Thread-0:- 2^4 value: 16
Thread-2: – 5^5 value: 3125
Thread-0: – 2^5 value: 32
Thread-3:- 8^1 value: 8
Thread-1:- 3^1 value: 3
Thread-3:- 8^2 value: 64
Thread-1:- 3^2 value: 9
Thread-3:- 8^3 value: 512
Thread-1:- 3^3 value: 27
Thread-3:- 8^4 value: 4096
Thread-1:- 3^4 value: 81
Thread-3:- 8^5 value: 32768
Thread-1:- 3^5 value: 243
If you observe the above results Thread-0, Thread-1 belongs to object-1 and Thread-2, Thread-3 are belonging to Object-2. So, there is no interference between thread 0 and 1 because of the same object (obj1). In the same way, there is no interference between Thread 2 and 3 because they belong to the same object (obj2). But if you observe there is interference between Thread 0 and 2, same as there is interference between Thread 1 and 3. To rectify this problem we will use static synchronization.
Program with static synchronization:
class Power{
synchronized static void printPower(int n){ //static synchronized method
int temp = 1;
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName() + ":- " +n + "^"+ i + " value: " + n*temp);
temp = n*temp;
try{
Thread.sleep(400);
}catch(Exception e){}
}
}
}
class Thread1 extends Thread{
Power p;
Thread1(Power p){
this.p=p;
}
public void run(){
p.printPower(2);
}
}
class Thread2 extends Thread{
Power p;
Thread2(Power p){
this.p=p;
}
public void run(){
p.printPower(3);
}
}
class Thread3 extends Thread{
Power p;
Thread3(Power p){
this.p=p;
}
public void run(){
p.printPower(5);
}
}
class Thread4 extends Thread{
Power p;
Thread4(Power p){
this.p=p;
}
public void run(){
p.printPower(8);
}
}
public class Synchronization_Example4{
public static void main(String args[]){
Power ob1 = new Power(); //first object
Power ob2 = new Power(); //second object
Thread1 p1 = new Thread1(ob1);
Thread2 p2 = new Thread2(ob1);
Thread3 p3 = new Thread3(ob2);
Thread4 p4 = new Thread4(ob2);
p1.start();
p2.start();
p3.start();
p4.start();
}
}
Output:
Thread-0:- 2^1 value: 2
Thread-0:- 2^2 value: 4
Thread-0:- 2^3 value: 8
Thread-0:- 2^4 value: 16
Thread-0:- 2^5 value: 32
Thread-1:- 3^1 value: 3
Thread-1:- 3^2 value: 9
Thread-1:- 3^3 value: 27
Thread-1:- 3^4 value: 81
Thread-1:- 3^5 value: 243
Thread-2:- 5^1 value: 5
Thread-2:- 5^2 value: 25
Thread-2:- 5^3 value: 125
Thread-2:- 5^4 value: 625
Thread-2:- 5^5 value: 3125
Thread-3:- 8^1 value: 8
Thread-3:- 8^2 value: 64
Thread-3:- 8^3 value: 512
Thread-3:- 8^4 value: 4096
Thread-3:- 8^5 value: 32768
In this static synchronization, we can observe there is no interference between Thread-0 and Thread-2 same as there is no interference between Thread-1 and 3. The next thread is executing after the previous thread completion or releasing lock only.
Inter – Thread communication or cooperation is a communication of two or more threads with each other. It can be done by using the following methods.
Why we need Inter – Thread Communication?
Example:
class Power{
void printPower(int n){
int temp = 1;
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName() + ":- " +n + "^"+ i + " value: " + n*temp);
temp = n*temp;
try{
this.wait(); //wait placed outside of the synchronized block or method
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
}
}
}
Output:
Thread-0:- 5^1 value: 5
java.lang.IllegalMonitorStateException
Thread-0:- 5^2 value: 25
java.lang.IllegalMonitorStateException
Thread-0:- 5^3 value: 125
java.lang.IllegalMonitorStateException
Thread-0:- 5^4 value: 625
java.lang.IllegalMonitorStateException
Thread-0:- 5^5 value: 3125
java.lang.IllegalMonitorStateException
Thread-1:- 8^1 value: 8
java.lang.IllegalMonitorStateException
Thread-1:- 8^2 value: 64
java.lang.IllegalMonitorStateException
Thread-1:- 8^3 value: 512
java.lang.IllegalMonitorStateException
Thread-1:- 8^4 value: 4096
java.lang.IllegalMonitorStateException
Thread-1:- 8^5 value: 32768
java.lang.IllegalMonitorStateException
Syntax:
public final void notify()
Syntax:
public final void notifyAll()
Synchronization Mechanism shows less performance.
Let’s consider an example, if there are five process P1, P2, P3, P4, P5 that are waiting to get the shared resources to access only one thread at a time so, all other processes are in waiting condition, the last process has to wait until all other processes to be complete. So, we have to use the synchronization concept where we will get inconsistent results.
Original article source at: https://www.mygreatlearning.com
1600135200
OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE). It contains the virtual machine, the Java Class Library, and the Java compiler. The difference between the Oracle OpenJDK and Oracle JDK is that OpenJDK is a source code reference point for the open-source model. Simultaneously, the Oracle JDK is a continuation or advanced model of the OpenJDK, which is not open source and requires a license to use.
In this article, we will be installing OpenJDK on Centos 8.
#tutorials #alternatives #centos #centos 8 #configuration #dnf #frameworks #java #java development kit #java ee #java environment variables #java framework #java jdk #java jre #java platform #java sdk #java se #jdk #jre #open java development kit #open source #openjdk #openjdk 11 #openjdk 8 #openjdk runtime environment
1620458875
According to some surveys, such as JetBrains’s great survey, Java 8 is currently the most used version of Java, despite being a 2014 release.
What you are reading is one in a series of articles titled ‘Going beyond Java 8,’ inspired by the contents of my book, Java for Aliens. These articles will guide you step-by-step through the most important features introduced to the language, starting from version 9. The aim is to make you aware of how important it is to move forward from Java 8, explaining the enormous advantages that the latest versions of the language offer.
In this article, we will talk about the most important new feature introduced with Java 10. Officially called local variable type inference, this feature is better known as the **introduction of the word **var
. Despite the complicated name, it is actually quite a simple feature to use. However, some observations need to be made before we can see the impact that the introduction of the word var
has on other pre-existing characteristics.
#java #java 11 #java 10 #java 12 #var #java 14 #java 13 #java 15 #verbosity
1624479720
Java is a type-safe programming language. Type safety ensures a layer of validity and robustness in a programming language. It is a key part of Java’s security to ensure that operations done on an object are only performed if the type of the object supports it.
Type safety dramatically reduces the number of programming errors that might occur during runtime, involving all kinds of errors linked to type mismatches. Instead, these types of errors are caught during compile-time which is much better than catching errors during runtime, allowing developers to have less unexpected and unplanned trips to the good old debugger.
Type safety is also interchangeably called strong typing.
Java Generics is a solution designed to reinforce the type safety that Java was designed to have. Generics allow types to be parameterized onto methods and classes and introduces a new layer of abstraction for formal parameters. This will be explained in detail later on.
There are many advantages of using generics in Java. Implementing generics into your code can greatly improve its overall quality by preventing unprecedented runtime errors involving data types and typecasting.
This guide will demonstrate the declaration, implementation, use-cases, and benefits of generics in Java.
#java #guide to understanding generics in java #generics #generics in java #guide to understanding generics in java
1620462686
On March 16th, 2021, Java 16 was GA. With this new release, tons of new exciting features have been added. Check out the release notes to know more about these changes in detail. This article’s focus will be on Java Records, which got delivered with JEP 395. Records were first introduced in JDK 14 as a preview feature proposed by JEP 359, and with JDK 15, they remained in preview with JEP 384. However, with JDK 16, Records are no longer in preview.
I have picked Records because they are definitely the most favored feature added in Java 16, according to this Twitter poll by Java Champion Mala Gupta.
I also conducted a similar survey, but it was focused on features from Java 8 onwards. The results were not unexpected, as Java 8 is still widely used. Very unfortunate, though, as tons of new features and improvements are added to newer Java versions. But in terms of features, Java 8 was definitely a game-changer from a developer perspective.
So let’s discuss what the fuss is about Java Records.
#java #springboot #java programming #records #java tutorials #java programmer #java records #java 16
1602637135
Java is not the hardest language to start with. So, it becomes way popular among novice developers joining the ranks of Java coders every single day. If you are reading this blog post, you might be interested in learning Java.
Java is widely used across industry, and especially in the area of Enterprise software, which results in many high paying job opportunities and makes this programming language a common language for newbies. A general promotion of it within colleges and other institutions providing a formal Computer Science education also contributes to its popularity.
However, these are not the only advantages of Java — among other things, it allows you to adopt good practices and makes it way easier to learn other languages in the future. And with no doubt, you can easily learn it if you’re following the right approach. In this post, I am going to share some of them with you.
Beyond all doubt, practice is important and valuable. But, before we get to the advantages of hands-on experience, I want to draw your attention to one essential thing I often tell my students.
New programmers who are just learning and start implementing things, without being supervised, often end up adapting bad practices. To avoid that, especially when you are making your first steps in programming, I recommend looking for a person who will supervise you and teach you. A strong mentorship with someone engaged in a serious project, as well as communication within the community in the form of sharing code and asking for feedback, is worth the effort. Similarly, when you are applying for your first job, you want to be looking for a company with a strong team and a good leader who would be keen on investing into your learning.
Now, let’s return to practical experience. Learning by doing is different from learning by passively consuming the information. To make sure we can use all the newly acquired technology, we should put our skills to test and write tons of code. The benefits of hands-on experience are almost endless.
By practicing, you get a clear understanding of what programming is. Consequently, you start doing better with each new hands-on task, complete it faster, and thus become more productive.
Even if you are not working on real-world projects yet, it’s important to get used to having deadlines. They are inextricably linked to the programming process. My recommendation is to set up your own deadlines while practicing stage and follow them as closely as possible.
#java #learn java #java code #learn java in easy way #learn java course #learn java development