Drop Down

Monday, January 6, 2020

ReentrantLock_Demo3 (trylock)

package multithreading;

import java.util.concurrent.locks.ReentrantLock;

class MyyThread extends Thread
{
static ReentrantLock l = new ReentrantLock();
MyyThread(String name)
{
super(name);
}
@Override
public void run()
{
if(l.tryLock())
{
System.out.println(Thread.currentThread().getName() + " ..got lock and performing safe operation.");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
l.unlock();
}
else
{
System.out.println(Thread.currentThread().getName() + " ..unable to get lock and performing alternative operation.");
}
}
}
public class ReentrantLock_Demo3
{
public static void main(String[] args)
{
MyyThread mt = new MyyThread("F1 Thread");
MyyThread mt2 = new MyyThread("F2 Thread");
mt.start();
mt2.start();
}

}


-----------------------------------------------------------
output:
---------
F1 Thread ..got lock and performing safe operation.
F2 Thread ..unable to get lock and performing alternative operation.

Synchronized Demo_3 (Using ReentrantLock)

package multithreading;

import java.util.concurrent.locks.ReentrantLock;

class Displays
{
ReentrantLock l = new ReentrantLock();
public void wish(String name)
{
l.lock();
for(int i=0;i<10;i++)
{
System.out.print("GoodMorning: ");
try
{
     Thread.sleep(2000);
    }
catch(InterruptedException e)
    {
    e.printStackTrace();
    }
System.out.print(name);
System.out.println();
}
l.unlock();
}
}

class MyThread6 extends Thread
{
String name;
Displays d;
MyThread6(String name, Displays d)
{
this.name = name;
this.d = d;
}
public void run()
{
d.wish(name);
}
}

public class Synchronized_Using_ReentrantLock
{
public static void main(String[] args)
{
Display d = new Display();
MyThread5 mt1 = new MyThread5("Dhoni", d);
MyThread5 mt2 = new MyThread5("Yuvraj", d);
mt1.start();
mt2.start();
}

}
----------------------------------------------------------
output:
---------

GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj



Synchronized Demo_2

package multithreading;

class Display
{
//public void wish(String name)
public synchronized void wish(String name)
{
for(int i=0;i<10;i++)
{
System.out.print("GoodMorning: ");
try
{
     Thread.sleep(2000);
    }
catch(InterruptedException e)
    {
    e.printStackTrace();
    }
System.out.print(name);
System.out.println();
}
}
}

class MyThread5 extends Thread
{
String name;
Display d;
MyThread5(String name, Display d)
{
this.name = name;
this.d = d;
}
public void run()
{
d.wish(name);
}
}

public class SynchronizedDemo
{
public static void main(String[] args)
{
Display d = new Display();
MyThread5 mt1 = new MyThread5("Dhoni", d);
MyThread5 mt2 = new MyThread5("Yuvraj", d);
mt1.start();
mt2.start();
}

}

//Here we are making wish method as synchronized so at a time only one thread can access wish method, this is to handle Race Condition.

---------------------------------------------------------------------------------------
output:
---------

GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj

Synchronized Demo_1

package multithreading;

class Display
{
public void wish(String name)
{
for(int i=0;i<10;i++)
{
System.out.print("GoodMorning: ");
try
{
     Thread.sleep(2000);
    }
catch(InterruptedException e)
    {
    e.printStackTrace();
    }
System.out.print(name);
System.out.println();
}
}
}

class MyThread5 extends Thread
{
String name;
Display d;
MyThread5(String name, Display d)
{
this.name = name;
this.d = d;
}
public void run()
{
d.wish(name);
}
}

public class SynchronizedDemo
{
public static void main(String[] args)
{
Display d = new Display();
MyThread5 mt1 = new MyThread5("Dhoni", d);
MyThread5 mt2 = new MyThread5("Yuvraj", d);
mt1.start();
mt2.start();
}

}

//Here on Same object of Display 'd' two threads are accessing wish()
//so we get irregular output

----------------------------
output (here we get irrigular output)

GoodMorning: GoodMorning: DhoniYuvraj
GoodMorning:
GoodMorning: DhoniYuvraj
GoodMorning:
GoodMorning: DhoniYuvraj
GoodMorning:
GoodMorning: YuvrajDhoni
GoodMorning:
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
Yuvraj


Join Example

package multithreading;

class Test1 extends Thread
{
static Thread ChildTh;
@Override
  public void run()
  {
/* try
{
ChildTh.join(); // waiting for the main thread to complete
}
catch (InterruptedException e)
{
e.printStackTrace();
} */
    for(int i = 65;i<75;i++)
    {
    System.out.print((char)i+" ");
    }
  }
}

public class Join_Exp
{

public static void main(String[] args)
{
Test1.ChildTh= Thread.currentThread();
Test1 t = new Test1();
t.start();
try
{
t.join();  // waiting for child thread to complete
}
catch (InterruptedException e)
{
e.printStackTrace();
}
for(int i = 65;i<75;i++)
  {
  System.out.print(i+" ");
  }

}

}

Thread Group Example

package multithreading;

public class Adv_Thread1 {

public static void main(String[] args)
{
  ThreadGroup g1 = new ThreadGroup("First Group");
  System.out.println(g1.getParent().getName());
 
  ThreadGroup g2 = new ThreadGroup(g1,"Second group ");
  System.out.println(g2.getParent().getName());
 
  ThreadGroup g3 = new ThreadGroup(g1,"Third Group");
  System.out.println(g3.getParent().getName());
 
  Thread t1 = new Thread(g1, "thread_01");
  Thread t2 = new Thread(g1, "thread_02");
  Thread t3 = new Thread(g1, "thread_03");  
  Thread t4 = new Thread(g2, "thread_04");
 
  g1.setMaxPriority(3);
  Thread t5 = new Thread(g1, "thread_05");
  Thread t6 = new Thread(g1, "thread_06");
  System.out.println(t1.getPriority());
  System.out.println(t2.getPriority());
  System.out.println(t3.getPriority());
  System.out.println(t4.getPriority());
 
  System.out.println(t5.getPriority());
  System.out.println(t6.getPriorit:y());
 

}

}
--------------------------------------------------------------------------
output:

main
First Group
First Group
5
5
5
5
3
3

Check System threads

package multithreading;

public class System_Threads
{
public static void main(String[] args)
{
  System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
  System.out.println("*********************************************");
  ThreadGroup gr = Thread.currentThread().getThreadGroup().getParent();
  Thread [] arr = new Thread[gr.activeCount()];
  gr.enumerate(arr);
  for(Thread t1 : arr)
  {
  System.out.println(t1.getName()+"~~"+t1.isDaemon());
  }
}

}

_________________________________________________________
output:

system
*********************************************
Reference Handler~~true
Finalizer~~true
Signal Dispatcher~~true
Attach Listener~~true
main~~false

Java 8 Notes Pics