Friday, 18 August 2017

Java Multithreading – Learn Everything That You Should Know part 2 ~ foundjava

Let’s See A Sample Code For IsDaemon() And SetDaemon() Method.
Thread CurrentThread():
It returns the instance reference of the currently executing thread.
Thread.State GetState():
It returns the state of the thread.
Let’s See A Sample Code.
Sample Output.
Yield():
This method causes the currently executing thread object to pause temporarily and allow other threads to run.
Let’s see a sample code.
The thread will halt after printing “In Run” due to the yield() method; since there is no other thread to execute so this thread will resume and print “Leaving run”.
Final Int GetPriority():
It returns the priority of the thread.
Final Void SetPriority(Int Priority):
This function is used to change the priority of a thread.
Let’s see a sample code.
Int GetId():
It returns the id of the thread.
Interrupt():
It interrupts the thread.
Boolean IsInterrupted():
tests if the thread has been interrupted and returns the interrupted flag either true or false.
Boolean Interrupted():
The static interrupted() method tests if the thread has been interrupted.This method returns the interrupted flag after that it sets the flag to false if it is true.
Let’s see a sample code.
Suspend():
You can use it to suspend the thread. [deprecated]
Resume():
You can use it to resume the suspended thread. [deprecated]
Stop():
You can use it to halt the thread. [deprecated]
Note: Sun has deprecated a variety of Thread methods, such as suspend(), resume() and stop() because they can lock up your programs or damage objects. As a result, you should not call them in your code.
We’ve covered almost all the important areas of Java Thread class in the above section, hope it would help. In the next segment, you’ll see the two methods to create threads in Java.

Java Thread Creation Methods And Examples.

Create Thread By Extending The Thread Class.

In this case, you need to complete the following steps to spawn a thread in a Java program.
  • Add a new class that extends the Thread class.
  • This newly created class should override the Run() method which is the entry point for the new thread.
  • Invokes the start() method to initiate the execution of the thread.
Let’s see a sample code as an example.

 Create Thread By Implementing The Runnable Interface.

For this approach, you need to follow the below steps to create a thread.
  • Create a class that does the following.
    • Implements the Runnable interface.
    • Provides the implementation of the run() method.
  • The run() method is an entry point for the thread and it remains alive till the run() method finishes its execution.
  • Once you create the thread, bring it to the Running state by calling the start() method.
    • Note: The start() method calls the run() method implicitly.
Let’s see a sample code as an example.

Download Java Multithreading Code Samples.

Now it’s time to download the sample projects so that you can easily understand the Java multithreading code snippets specified in this post. Please use the below link to start your download.


If you don’t have Java SDK it on your system, please click here to download it. In case, you don’t already have Eclipse IDE then refer the link to get it now.
Summary – Java Multithreading.
We wish that the above Java multithreading tutorial would have helped you in walking a step further on the Java learning scale. In the next article of Java programming, we’ll give insights on different ways to implement synchronization in Java applications.
In case, this post was able to intrigue you then please share it with your friends or float on social media using the below share icons.

No comments:

Post a Comment