Drop Down

Sunday, January 12, 2020

Get All Methods of Objects Class

package warmUP;

import java.lang.reflect.Method;

public class Show_Methods {

public static void main(String[] args) throws ClassNotFoundException
{
int count = 0;
Class c = Class.forName("java.lang.Object");
Method[] m = c.getDeclaredMethods();
for(Method m1 : m)
{
count++;
System.out.println(m1.getName());
}
System.out.println("Total No. of Methods: "+count);
}

}
----------------------------------------------------------------
OutPut:
---------
finalize
wait
wait
wait
equals
toString
hashCode
getClass
clone
notify
notifyAll
registerNatives
Total No. of Methods: 12


Different ways to create objects in Java

ways to create objects in Java


1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
MyObject object = new MyObject();

2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

 3. Using clone()The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject(); 
MyObject object = anotherObject.clone();

4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream ); 
MyObject object = (MyObject) inStream.readObject();
------------------------------------------------------------------------------------------------------------------------------------.

Monday, January 6, 2020

TryLock game

package multithreading;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

class MyThread4 extends Thread
{
static ReentrantLock l = new ReentrantLock();
MyThread4(String name)
{
super(name);
}
@Override
public void run()
{
do
{
try {
if(l.tryLock(5000,TimeUnit.MICROSECONDS))
{
System.out.println(Thread.currentThread().getName()+" ..Got Lock and running safe code");
Thread.sleep(30000);
l.unlock();
System.out.println(Thread.currentThread().getName()+" ..Released the lock");
break;
}
else
{
System.out.println(Thread.currentThread().getName()+" ..Unable to get Lock and executing alternative code");
Thread.sleep(5000);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
while(true);
}
}


public class ReentrantLock_Demo4
{

public static void main(String[] args)
{
MyThread4  mtt1 = new MyThread4("F1 Thread");
MyThread4  mtt2 = new MyThread4("F2 Thread");
mtt1.start();
mtt2.start();
}

}
----------------------------------
output
----------
F2 Thread ..Got Lock and running safe code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F2 Thread ..Released the lock
F1 Thread ..Got Lock and running safe code
F1 Thread ..Released the lock

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