Showing posts with label thread. Show all posts
Showing posts with label thread. Show all posts

Determine thread hold synchronized lock in java

Source code below will print true if current thread hold synchronized lock for current object and print false for otherwise.

*******************************************
COMPLETE SOURCE CODE FOR : DetermineThreadHoldSynchronizedLock.java
*******************************************


//This program will print three value whether current thread hold synchronized lock
//for cuurent object or not.
public class DetermineThreadHoldSynchronizedLock
{
boolean isLock;

public DetermineThreadHoldSynchronizedLock()
{
//Check whether current thread hold synchronized lock for current object
isLock=Thread.holdsLock(this);

//Print lock value
//It should print false
System.out.println("HOLD SYNCHRONIZED : "+isLock);

//Go to method locker()
locker();
}

public void locker()
{
//Current thread get lock from current object
//It's mean other thread can't interrupt untill this thread get out from
//synchronized block,{}...like below
synchronized (this)
{
isLock=Thread.holdsLock(this);
//Print lock value
//It should print true
System.out.println("HOLD SYNCHRONIZED : "+isLock);
}
lastCheck();
}

public void lastCheck()
{
//Check whether current thread hold synchronized lock for current object
isLock=Thread.holdsLock(this);
//Print lock value
//It should print false
System.out.println("HOLD SYNCHRONIZED : "+isLock);
}

public static void main(String[]args)
{
DetermineThreadHoldSynchronizedLock dthsl=new DetermineThreadHoldSynchronizedLock();
}
}


*******************************************
JUST COMPILE AND EXECUTE IT
*******************************************

Pause thread in java

Source code below will print hi after pause for 5 seconds.

********************************************************
COMPLETE SOURCE CODE FOR : ThreadPause.java
********************************************************


public class ThreadPause extends Thread
{
public void run()
{
//Pause thread for 5 seconds before print hi
try
{
sleep(5000);
}
catch(Exception exception)
{
exception.printStackTrace();
}

//Print hi
System.out.println("hi");
}

public static void main(String[]args)
{
//Create a thread by create ThreadPause object
ThreadPause a=new ThreadPause();

//Start the thread
a.start();
}
}


********************************************************
JUST COMPILE AND EXECUTE IT
********************************************************

Stop thread in java

***********************************************
COMPLETE SOURCE CODE FOR : StopThread.java
***********************************************


import javax.swing.JOptionPane;

public class StopThread implements Runnable
{
int currentValue=0;

public void run()
{
while(true)
{
JOptionPane.showMessageDialog(null,"currentValue = "+currentValue);

String temp=JOptionPane.showInputDialog(null,"CHOOSE 1 OR 2\n1-Continue Thread\n2-Stop Thread");

if(temp.equals("2"))
{
JOptionPane.showMessageDialog(null,"Thread Stop, Bye");
return;//This will make thread stop
}
else if(temp.equals("1"))
{
currentValue=currentValue+1;
JOptionPane.showMessageDialog(null,"Please stop me, i'm tired\ncurrentValue will add one");
}
else
{
JOptionPane.showMessageDialog(null,"I don't know your input");
}
}
}

public static void main(String[]args)
{
//Create a thread by create StopThread object
StopThread a=new StopThread();

//run thread
a.run();
}
}


***********************************************
JUST COMPILE AND EXECUTE IT
***********************************************

Create thread in java

Create thread in java

COMPLETE SOURCE CODE FOR CreateThread.java


public class CreateThread implements Runnable
{
//all thread implementation will be put in run method
public void run()
{
//Print hi
System.out.println("HI");
}

public static void main(String[]args)
{
//Create thread by create CreateThread object
CreateThread a=new CreateThread();

//run the thread
a.run();
}
}


JUST COMPILE AND EXECUTE IT

RELAXING NATURE VIDEO