Showing posts with label Thread. Show all posts
Showing posts with label Thread. Show all posts

Friday, February 3, 2023

Java thread synchronization

Do you know what is thread synchronization Java? Have you ever thought about how to use synchronized in Java? let us gain some knowledge about thread synchronization Java & try to find out the answer to what is thread synchronization in Java.

But before we start writing about thread synchronization Java & try to find out how to use synchronized in Java. Let us first try to understand the policy of synchronization from one daily life example.

Assume one daily life scenario.

Suppose in your school, there is one toilet for the gents. Now, at the same time, you & your friend need to access the toilet. In both cases, the matter is an urgent one. So, you are not ready to let him go. Neither of your friends is ready to let you go. As a result, there is a mess in front of the toilet.

So, what you will do in such a case? Or what your friend will do at that time?

So, you called the teacher to solve the situation that arise there. Your teacher will draw a solution & allow anyone to go to the toilet first. And as a teacher is a respected person both of you are ready with the solution. In this case, the teacher helps to solve the conflict.

The same thing happens in thread synchronization Java. When two different parts of the program try to access the same resources then this trouble arises. We will try to know more about it when we discuss what is thread synchronization in Java.

What Is Thread Synchronization in Java:

Java is an important programming language. Java is used in the corporate world as it helps to solve real-life problems very easily. Java is used for game development purposes. These along with an operation some more processes are going on. For implementing that situation, Java programming language used the thread concept. Thread is a special concept of the Java programming language. It helps to execute more operations a t same time.

Synchronization means collaborating two or more processes at the same time. The thread synchronization Java is quite like this. Now, sometimes when two or more processes are using the same resource then there will be a problem. This problem arises when we use thread in Java. Two or more parts of the code want to use the same resources. Then there is a mess at that point.

The main goal of thread synchronization in Java is to remove the problems related to resources. If two or more parts want to use the same resource, then one by one they will execute that. This means one part will use that resource, then it will be removed. After that, another part is going to use the resource & will be removed. This is a very problematic situation. To come out of this situation, we used the 'synchronized' keyword.

Synchronize is like the teacher at your school. It helps to remove the conflicts between two or more parts for using the same recourses. We will find out more about this when we implement the thread synchronized Java. Similarly, when you are stuck at Java coding you can use Java Assignment Help Services from codingzap.

Why Should We Use Thread Synchronization Java:

Now, after knowing about thread synchronization Java, we need to know why we should use it. This is a very important topic. Often, we find out that, the threads that are implemented in the program are not executing the same we want. In those cases, we need to use this method. As there is a problem related to the synchronization.

If we don't use synchronization, then there will be an issue related to the memory space. Also, the flow of the output of the program will not be similar as desired. There will be a thread interface problem if we don't use synchronization at the correct time. When we implement thread synchronization Java, it will be easy to understand.

Wednesday, November 13, 2013

Java program to play an audio sound

These are the following ways in which you play a sound in Java.

Toolkit - If you just want a beep or quick alert you can use beep() finction of class java.awt.Toolkit. It Emits an audio beep.
AudioClip - The AudioClip interface is a simple abstraction for playing a sound clip.
Clip - The Java Sound API provides functionality for the capture, processing, and playback of sampled audio data & the sequencing and synthesis of MIDI data.
MIDI Sequences - The javax.sound.midi package provides an interfaces and classes for I/O, sequencing, and synthesis of MIDI (Musical Instrument Digital Interface) data.

Please see the sample code of each type


Tuesday, February 7, 2012

Java Threads - Wait and Notify Example

This is a simple Thread example which demonstrates wait and notify functionality of a Thread. In this example we have a main class i.e. MainThread which will call AnotherThread, so that you can see both running simultaneously. Now our main thread enters into wait state till AnotherThread notifies it.


Please see the self explanatory code. Directly you can this code and see the output.


MainThread.java



/* MainThread.java */


public class MainThread {


public static void main(String[] args) {

//Start another thread
Thread anotherThreadObject = new Thread(new AnotherThread());
anotherThreadObject.start();

//Main thread continues
for(int i=0;i<10;i++) {

System.out.println(Thread.currentThread().getName()+" : "+i);

//this thread halts for 500 millisecond
try {
Thread.sleep(1 * 500);
} catch (InterruptedException e) {
e.printStackTrace();
}

//when counter reaches 5 wait for Other thread to notify it
if(i == 5) {
try {
synchronized(MainThread.class) {
System.out.println("MainThread is Waiting....");
MainThread.class.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}//for
}//main


}

Friday, August 21, 2009

Java : Simple Thread Example

How to start new thread from main thread. Any program starts execution from main thread how ever you can start n numbers of threads from this main thread. Lets see a simple Thread example, just compile and run main program and see output.

/* MainClass.java */

public class MainClass
{
public static void main(String[] args)
{

System.out.println("Main Thread Started");

AnotherClass ac = new AnotherClass();
//Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
ac.start();

for(int i=0;i<=10 ;i++ )
{
System.out.println("Main "+i);

try
{
//Get current Thread
Thread.currentThread().sleep(300);
}
catch (InterruptedException ex)
{
//sleep() method throws this checked exception
System.out.println("Exception in MainClass thread : "+ex);
}

}
System.out.println("Ending Main Thread");
}
}


/* AnotherClass.java */


//Inheriting from thread class
public class AnotherClass extends Thread
{
//Overrideing rum method from thread class
public void run()
{
System.out.println("Another Thread Started");

for (int j=0;j<=10 ;j++ )
{
System.out.println("Another "+j);

try
{
//sleep() method can be used here directly becz it is publicly inherited from Thread class
sleep(100);
}
catch (InterruptedException ex)
{
//Handling InterruptedException rather to declare becz overriding function cannot throw any new checked exception
System.out.println("Exception in AnotherClass thread : "+ex);
}
}
System.out.println("Ending Another Thread");
}
}



/**********OUTPUT***************/

Main Thread Started
Main 0
Another Thread Started
Another 0
Another 1
Another 2
Main 1
Another 3
Another 4
Another 5
Main 2
Another 6
Another 7
Another 8
Main 3
Another 9
Another 10
Ending Another Thread
Main 4
Main 5
Main 6
Main 7
Main 8
Main 9
Main 10
Ending Main Thread