Thread Tutorials in Java? Thread example in Java, What is Thread?

15
Thread is a collection of light-weight subprocess to execute the program, Java each program managed by the main thread because Java program execution will be started by main().
Thread Concept generally used to provide a multi-threading system in an application.
Multithreading is the subpart of multitasking using this we can execute multiple processes under a single resource. we can save the resource for application.
For example, if we assign multiple tasks to a single developer at the same time that is called multithreading.
if we assign five different tasks to five different developers at the same time that is not multithreading, it is called multiprocessing.
Multithreading saves resources (memory, effort,  time) but multiprocessing takes more resources as compare to multithreading.
Thread life cycle:-
When we create a thread program manually in Java then the thread objects will follow various groups of states step by step.
1) init state:-   when we create Thread class object then it is in init State
2)  ready state:-   It is the Thread state before thread execution
3)  runnable state:-  When we call start() then the thread will be in a runnable state
4)   not runnable state:-  when we call wait() or sleep() then the thread will be in a non-runnable state
5)   completion state or termination state:-  When the threading process will be completed then the thread will be in completion state.



Create a Simple Program to implement the Single Thread process:-
class ThreadExample extends Thread
{
   public void run()
   {
       for(int i=1;i<=10;i++)
       {
             try
             {
             System.out.println(i);
             Thread.sleep(1000);
             }
             catch(Exception ex)
             {
             }
       }
   }
public static void main(String args[])
{
    ThreadExample obj = new ThreadExample();
    obj.start();
}
}
How many ways to implement Thread?
We can implement Thread using two different ways
1)  using Thread Class:-
     class Classname extends Thread
   {
           public void run()  //   optional
          {
          }
   }
limitation:-  Thread class can not manage multiple inheritances. for example, we want to extend any other class functionality as well as Thread functionality then it is not possible by Thread class.
2)  using Runnable Interface:-
This is the interface which provides run() to manage the running state of the thread, we can easily perform multiple Inheritance using Runnable Interface.
class Classname implements Runnable    //it is not complete thread class
{
        Classname()
       {
            Thread  t= new Thread(this);   //Mandatory to create thread object
            t.start();
       }
        public void run()      //mandatory
        {
        }
}
Example of the runnable interface:-
class A
{
    public String display()
    {
       return "A";
    }
}
class ThreadExample2  extends A implements Runnable
{
      ThreadExample2()
      {
           Thread t = new Thread(this);
           t.start();
      }
      public void run()
      {
          for(int i=1;i<=10;i++)
          {
              try
              {
                  System.out.println(i+super.display());
                  Thread.sleep(1000);
              }
              catch(InterruptedException ex)
              {

              }
          }
      }
  public static void main(String args[])
  {
       ThreadExample2 obj = new ThreadExample2();
  }
}
.................................................................................................................................................

Type of Thread:-

1) Single Thread:-  using this we will start one thread process at a time means if we create only one thread object and start them then it is called Single Thread.
 public static void main(String args[])
  {
       ThreadExample2 obj = new ThreadExample2();
      }
2)  MultiThread:-   using this we can execute multiple Thread processes at the same time and the same resource then it is called multithreading.
if we create multiple Thread Object and start the threading process then it will implement the multithreading concept.
By default multithreading process provide parallel execution, using Thread Synchronization we can provide a Multithreading parallel process to step by step.
The program is the same only we create multiple objects.
 public static void main(String args[])
  {
       ThreadExample2 obj = new ThreadExample2();
       ThreadExample2 obj1 = new ThreadExample2();
       ThreadExample2 obj2 = new ThreadExample2();
  }
.............................................................................................................................................................

Thread Synchronization:-
Using this we will perform step-by-step execution of parallel thread means when the First thread will be executed completely then another thread will be entered into the environment.
Thread Synchronization will be implemented by Synchronized keywords to implement the synchronization process using the Synchronized method and Synchronized block.
Example of Thread Synchronization:-
class Table
{
   int num;
   synchronized void displayTable(int num)
   {
        for(int i=1;i<=10;i++)
        {
              try
              {
              System.out.println(num*i);
              Thread.sleep(1000);
              }
              catch(InterruptedException ex)
              {

              }
        }
   }
}
class Thread1 extends Thread
{   Table t;
    public Thread1(Table t)
    {
     this.t=t;   
    }

   public void run()
   {
        t.displayTable(5);
   }
}
class Thread2 extends Thread
{   Table t;
    public Thread2(Table t)
    {
     this.t=t;   
    }
   public void run()
   {
        t.displayTable(9);
   }
}
class ExecuteThread
{
   public static void main(String args[])
   {      Table t = new Table();
          Thread1 t1 = new Thread1(t);
          t1.start();
          Thread2 t2 = new Thread2(t);
          t2.start();
   }
}
Program to implement wait() and sleep() simultaneous in Single Program?
class Table
{
   int num;
   synchronized void displayTable(int num)
   {
        for(int i=1;i<=10;i++)
        {
              try
              {
              System.out.println(num*i);
              //Thread.sleep(1000);
                if(i>=5)
                wait(1000);
                else
                Thread.sleep(1000);
              }
              catch(InterruptedException ex)
              {
              }
        }
   }
}
class Thread1 extends Thread
{   Table t;
    public Thread1(Table t)
    {
     this.t=t;   
    }

   public void run()
   {
        t.displayTable(5);
   }
}
class Thread2 extends Thread
{   Table t;
    public Thread2(Table t)
    {
     this.t=t;   
    }
   public void run()
   {
        t.displayTable(9);
   }
}
class ExecuteThread
{
   public static void main(String args[])
   {      Table t = new Table();
          Thread1 t1 = new Thread1(t);
          t1.start();
          Thread2 t2 = new Thread2(t);
          t2.start();
   }
}
...................................................................................................................................................

Synchronized block example:-
if we want to synchronize a particular line of code of method then we can use a synchronized block.
because if we create a synchronized method then the complete method code will be synchronized, we can create more then one synchronized block in a single method.
class Table
{
   int num;
   void displayTable(int num)
   {
       synchronized(this)
       {
        for(int i=1;i<=10;i++)
        {   
              try
              {
              System.out.println(num*i);
              //Thread.sleep(1000);
                if(i>=5)
                wait(1000);
                else
                Thread.sleep(1000);
              }
              catch(InterruptedException ex)
              {
              }
        }
      }
   }
}
class Thread1 extends Thread
{   Table t;
    public Thread1(Table t)
    {
     this.t=t;   
    }
   public void run()
   {
        t.displayTable(5);
   }
}
class Thread2 extends Thread
{   Table t;
    public Thread2(Table t)
    {
     this.t=t;   
    }
   public void run()
   {
        t.displayTable(9);
   }
}
class ExecuteThread
{
   public static void main(String args[])
   {      Table t = new Table();
          Thread1 t1 = new Thread1(t);
          t1.start();
          Thread2 t2 = new Thread2(t);
          t2.start();
   }
}
..............................................................................................................................................
1 ) notify() and notifyAll():-  It is used to notify waiting for a time interval of thread means if we provide notify() under wait() then wait time will be finished automatically.
notify all() will resume all waiting state thread in application.it can be used with multiple synchronized blocks under thread.
Example of notify()
class Table
{
   int num;
   void displayTable(int num)
   {
       synchronized(this)
       {
        for(int i=1;i<=10;i++)
        {   
              try
              {
              System.out.println(num*i);
             wait(3000);
                notify();
                 }
              catch(InterruptedException ex)
              {
              }
        }
      }
   }
}
class Thread1 extends Thread
{   Table t;
    public Thread1(Table t)
    {
     this.t=t;   
    }
   public void run()
   {
        t.displayTable(5);
   }
}
class Thread2 extends Thread
{   Table t;
    public Thread2(Table t)
    {
     this.t=t;   
    }
   public void run()
   {
        t.displayTable(9);
   }
}
class ExecuteThread
{
   public static void main(String args[])
   {      Table t = new Table();
          Thread1 t1 = new Thread1(t);
          t1.start();
          Thread2 t2 = new Thread2(t);
          t2.start();
   }
}
Join():-  join is used to merge the process of thread, but it will merge another thread process when the currently executing thread will be completed.
Thread t1 = new Thread();
Thread t2 = new Thread();
t1.start();
t1.join();
t2.start();
Example of join():-
class ThreadExample extends Thread
{
   public void run()
   {
       for(int i=1;i<=10;i++)
       {
             try
             {
             System.out.println(i);
             Thread.sleep(1000);
             }
             catch(Exception ex)
             {
             }
       }
   }
public static void main(String args[]) throws InterruptedException
{
    ThreadExample obj = new ThreadExample();
    obj.start();
    obj.join(1000);
    ThreadExample obj1 = new ThreadExample();
    obj1.start();
}
}
Daemon Thread:-   It is called System Thread because it is used to execute all User-Define Thread under the application. Daemon Thread will be the Primary Thread of Java Application.
Java call garbage collector using the daemon thread method. This thread will be started automatically in the System and provide background support to all UserDefine Thread.
We can convert user-defined thread to daemon Thread using setDaemon().
 Example of Daemon Thread:-
class ThreadExample extends Thread
{
   public void run()
   {
       for(int i=1;i<=10;i++)
       {
             try
             {
             System.out.println(i);
             Thread.sleep(1000);
             }
             catch(Exception ex)
             {
             }
       }
   }
public static void main(String args[])
{
    ThreadExample obj = new ThreadExample();
    obj.setDaemon(true);
    obj.start();
    ThreadExample obj1 = new ThreadExample();
    obj1.start();
}
}
ASSIGNMENTS?
q1) Create Fibonacci series and table program using multithreading with thread synchronization.
Q2)  Create a Program to write the content and read the content simultaneously on file using multithreading with thread synchronization?
Tags

Post a Comment

15Comments

POST Answer of Questions and ASK to Doubt

  1. nitesh bamotriya
    class Factorial
    {
    int num;
    int fact=1;

    void displayFactorial(int num)
    {
    for(int i=0;i<10;i++)
    {
    try{

    System.out.println("factorial is"+fact=fact*i);
    Thread.sleep(2000);
    }catch(InterruptedException ex)
    {
    }
    }



    }

    }
    class Thread1
    {
    Fact f;
    public Thread1(Fact f)
    {
    this.f=f;
    }
    public void run()
    {
    f.displayFact(6);
    }

    }
    class Thread2
    {
    Fact f;
    public Thread2(Fact f)
    {
    this.f=f;
    }
    public void run()
    {
    f.displayFact(7);
    }

    }
    class Main
    {
    public static void main(String args[])
    {
    Factorial f=new Factorial();
    Thread1 t1=new Thread1(f);
    t1.start();
    Thread2 t2=new Thread2(f);
    t2.start();

    }
    }

    ReplyDelete
  2. //yogesh seroke
    //thread eg....

    class ThreadEg extends Thread
    {
    public void run()
    {
    for(int i=1;i<=10;i++)
    {
    try
    {
    System.out.println(i);
    Thread.sleep(1000);
    }
    catch(Exception ex)
    {

    }
    }
    }
    }
    class ThreadEgmain
    {
    public static void main(String args[])
    {
    ThreadEg obj = new ThreadEg();
    obj.start();
    }
    }

    ReplyDelete
  3. //yogesh seroke
    //thread eg...

    class Threadsi extends Thread
    {
    int p,r,t,si;
    public void accept(int p,int r,int t)
    {
    this.p=p;
    this.r=r;
    this.t=t;
    }
    public void fun()
    {
    si=(p*r*t)/100;
    System.out.println(si);
    for(int i=1;i<=10;i++)
    {
    try
    {
    System.out.println(i);
    Thread.sleep(2000);
    }
    catch(Exception ex)
    {

    }
    }
    }
    }

    class Threadsimain
    {
    public static void main(String args[])
    {
    Threadsi obj=new Threadsi();
    obj.accept(50000,3,2);
    obj.fun();
    obj.start();
    }
    }

    ReplyDelete
  4. //yogesh seroke
    //example of Runnable interface

    package threadexample;

    class A
    {
    public String display()
    {
    return "A";
    }
    }

    public class Threadexample1 extends A implements Runnable
    {
    Threadexample1()
    {
    Thread t=new Thread(this);
    t.start();
    }
    public void run()
    {
    for(int i=1;i<=10;i++)
    {
    try
    {
    System.out.println(i+super.display());
    Thread.sleep(1000);
    }
    catch(InterruptedException ex)
    {

    }
    }
    }
    public static void main(String args[])
    {
    Threadexample1 obj=new Threadexample1();
    }
    }

    ReplyDelete
  5. //yogesh seroke
    //example of thread without using synchronized keyword....

    package threadexample;

    class Pahade
    {
    int num;
    void displayTable(int num)
    {
    for(int i=1;i<=10;i++)
    {
    try
    {
    System.out.println(num*i);
    Thread.sleep(1000);
    }
    catch(InterruptedException ex)
    {

    }
    }
    }
    }

    class Thread11
    {
    Pahade t;
    public Thread11(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(1);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }

    class Thread22
    {
    Pahade t;
    public Thread22(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(2);
    }
    public void start() {
    // TODO Auto-generated method stub

    }

    }

    class Thread3
    {
    Pahade t;
    public Thread3(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(3);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread4
    {
    Pahade t;
    public Thread4(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(4);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread5
    {
    Pahade t;
    public Thread5(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(5);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread6
    {
    Pahade t;
    public Thread6(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(6);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread7
    {
    Pahade t;
    public Thread7(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(7);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread8
    {
    Pahade t;
    public Thread8(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(8);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread9
    {
    Pahade t;
    public Thread9(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(9);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread10
    {
    Pahade t;
    public Thread10(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(10);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    public class Synchronizationmain1
    {
    public static void main(String args[])
    {
    Pahade t=new Pahade();
    Thread11 t1=new Thread11(t);
    t1.start();
    Thread22 t2=new Thread22(t);
    t2.start();
    Thread3 t3=new Thread3(t);
    t3.start();
    Thread4 t4=new Thread4(t);
    t4.start();
    Thread5 t5=new Thread5(t);
    t5.start();
    Thread6 t6=new Thread6(t);
    t6.start();
    Thread7 t7=new Thread7(t);
    t7.start();
    Thread8 t8=new Thread8(t);
    t8.start();
    Thread9 t9=new Thread9(t);
    t9.start();
    Thread10 t10=new Thread10(t);
    t10.start();
    }
    }

    ReplyDelete
  6. //yogesh seroke
    //example of thread with synchronized keyword

    package threadexample;

    class Pahade
    {
    int num;
    synchronized void displayTable(int num)
    {
    for(int i=1;i<=10;i++)
    {
    try
    {
    System.out.println(num*i);
    Thread.sleep(1000);
    }
    catch(InterruptedException ex)
    {

    }
    }
    }
    }

    class Thread11
    {
    Pahade t;
    public Thread11(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(1);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }

    class Thread22
    {
    Pahade t;
    public Thread22(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(2);
    }
    public void start() {
    // TODO Auto-generated method stub

    }

    }

    class Thread3
    {
    Pahade t;
    public Thread3(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(3);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread4
    {
    Pahade t;
    public Thread4(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(4);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread5
    {
    Pahade t;
    public Thread5(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(5);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread6
    {
    Pahade t;
    public Thread6(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(6);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread7
    {
    Pahade t;
    public Thread7(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(7);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread8
    {
    Pahade t;
    public Thread8(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(8);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread9
    {
    Pahade t;
    public Thread9(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(9);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    class Thread10
    {
    Pahade t;
    public Thread10(Pahade t)
    {
    this.t=t;
    }
    public void run()
    {
    t.displayTable(10);
    }
    public void start() {
    // TODO Auto-generated method stub

    }
    }
    public class Synchronizationmain1
    {
    public static void main(String args[])
    {
    Pahade t=new Pahade();
    Thread11 t1=new Thread11(t);
    t1.start();
    Thread22 t2=new Thread22(t);
    t2.start();
    Thread3 t3=new Thread3(t);
    t3.start();
    Thread4 t4=new Thread4(t);
    t4.start();
    Thread5 t5=new Thread5(t);
    t5.start();
    Thread6 t6=new Thread6(t);
    t6.start();
    Thread7 t7=new Thread7(t);
    t7.start();
    Thread8 t8=new Thread8(t);
    t8.start();
    Thread9 t9=new Thread9(t);
    t9.start();
    Thread10 t10=new Thread10(t);
    t10.start();
    }
    }

    ReplyDelete
  7. //yogesh seroke
    //example of wait with value(time)

    package threadexample;

    import java.io.InterruptedIOException;

    class T
    {
    int num;
    void displayTable(int num) throws InterruptedException
    {
    synchronized(this)
    {
    for(int i=1;i<=10;i++)
    {
    System.out.println(num*i);
    wait(1000);
    }
    }
    }

    }
    class T1 extends Thread
    {
    T t;
    public T1(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(2);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    class T2 extends Thread
    {
    T t;
    public T2(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(21);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public class Synchronizationmain2 {

    public static void main(String[] args) {

    T t=new T();
    T1 t1=new T1(t);
    t1.start();
    T2 t2=new T2(t);
    t2.start();
    }

    }

    ReplyDelete
  8. //yogesh seroke
    //example of wait without value(time)

    package threadexample;

    import java.io.InterruptedIOException;

    class T
    {
    int num;
    void displayTable(int num) throws InterruptedException
    {
    synchronized(this)
    {
    for(int i=1;i<=10;i++)
    {
    System.out.println(num*i);
    wait();
    }
    }
    }

    }
    class T1 extends Thread
    {
    T t;
    public T1(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(2);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    class T2 extends Thread
    {
    T t;
    public T2(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(21);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public class Synchronizationmain2 {

    public static void main(String[] args) {

    T t=new T();
    T1 t1=new T1(t);
    t1.start();
    T2 t2=new T2(t);
    t2.start();
    }

    }

    ReplyDelete
  9. //yogesh seroke
    //example of wait() and sleep() togather

    package threadexample;

    import java.io.InterruptedIOException;

    class T
    {
    int num;
    void displayTable(int num) throws InterruptedException
    {
    synchronized(this)
    {
    for(int i=1;i<=10;i++)
    {
    System.out.println(num*i);
    if(i>=5)
    {
    wait(1000);
    }
    else
    {
    Thread.sleep(1000);
    }
    }
    }
    }

    }
    class T1 extends Thread
    {
    T t;
    public T1(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(2);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    class T2 extends Thread
    {
    T t;
    public T2(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(21);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public class Synchronizationmain2 {

    public static void main(String[] args) {

    T t=new T();
    T1 t1=new T1(t);
    t1.start();
    T2 t2=new T2(t);
    t2.start();
    }

    }

    ReplyDelete
  10. //yogesh seroke
    //exmple of notify()

    package threadexample;

    import java.io.InterruptedIOException;

    class T
    {
    int num;
    void displayTable(int num) throws InterruptedException
    {
    synchronized(this)
    {
    for(int i=1;i<=10;i++)
    {
    System.out.println(num*i);

    wait(1000);
    notify();
    }
    }
    }

    }
    class T1 extends Thread
    {
    T t;
    public T1(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(2);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    class T2 extends Thread
    {
    T t;
    public T2(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(21);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public class Synchronizationmain2 {

    public static void main(String[] args) {

    T t=new T();
    T1 t1=new T1(t);
    t1.start();
    T2 t2=new T2(t);
    t2.start();
    }

    }

    ReplyDelete
  11. //yogesh seroke
    //example of notifyAll()

    package threadexample;

    import java.io.InterruptedIOException;

    class T
    {
    int num;
    void displayTable(int num) throws InterruptedException
    {
    synchronized(this)
    {
    for(int i=1;i<=10;i++)
    {
    System.out.println(num*i);

    wait(1000);
    notifyAll();
    }
    }
    }

    }
    class T1 extends Thread
    {
    T t;
    public T1(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(2);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    class T2 extends Thread
    {
    T t;
    public T2(T t)
    {
    this.t=t;
    }
    public void run()
    {
    try {
    t.displayTable(21);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public class Synchronizationmain2 {

    public static void main(String[] args) {

    T t=new T();
    T1 t1=new T1(t);
    t1.start();
    T2 t2=new T2(t);
    t2.start();
    }

    }

    ReplyDelete
  12. //yogesh seroke
    //example of join method

    package threadexample;

    class A extends Thread
    {

    public void run()
    {
    for(int i=1;i<=10;i++)
    {
    try
    {
    System.out.println(i);
    Thread.sleep(1000);
    }
    catch(Exception ex)
    {

    }
    }
    }
    }

    public class Synchronizationmain3 {
    public static void main(String args[]) throws Exception
    {
    A a=new A();

    a.start();
    a.join(1000);
    A a1=new A();
    a1.start();
    }
    }

    ReplyDelete
  13. //yogesh seroke
    //example of daemon thread

    package threadexample;
    class Daemonexample extends Thread
    {
    public void run()
    {
    for(int i=1;i<=10;i++)
    {
    try
    {
    System.out.println(i);
    Thread.sleep(1000);
    }
    catch(Exception ex)
    {

    }
    }
    }
    }
    public class Daemonthread
    {
    public static void main(String args[])
    {
    Daemonexample obj=new Daemonexample();
    obj.setDaemon(true);
    obj.start();

    Daemonexample obj1=new Daemonexample();
    obj1.start();
    }
    }

    ReplyDelete
  14. //yogesh seroke
    //Assignment(1) Fibonacci & Table....

    package threadexample;

    class Fibonacci
    {
    int a=0,b=1,c=0,num;
    public void display(int num)
    {
    for(int i=1;i<=num;i++)
    {
    c=a+b;
    a=b;
    b=c;
    System.out.print(c+" ");
    System.out.println(num*i);
    }

    }
    }

    class Fthread extends Thread
    {
    Fibonacci num;
    public Fthread(Fibonacci num)
    {
    this.num=num;
    }
    public void run()
    {
    num.display(10);
    }
    }

    class Tthread extends Thread
    {
    Fibonacci num;
    public Tthread(Fibonacci num)
    {
    this.num=num;
    }
    public void run()
    {
    num.display(10);
    }
    }
    public class Assignment1 {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Fibonacci obj=new Fibonacci();
    Fthread obj1=new Fthread(obj);
    obj1.start();
    Tthread obj2=new Tthread(obj);
    obj2.start();
    }

    }


    ReplyDelete
  15. //yogesh seroke
    //Write and Read File using MultipleThreading

    package threadexample;
    import java.io.*;

    class FileWriteFileRead
    {
    File f;

    public void createFile(File f2)throws IOException
    {
    try
    {
    f=new File("c://yogeshserokethread//assignment.txt");
    if(!f.exists())
    {
    f.createNewFile();
    }
    Thread.sleep(1000);
    }
    catch(InterruptedException ex)
    {

    }
    }

    public void writeFile() throws IOException
    {
    try
    {
    FileWriter fw=new FileWriter(f);
    BufferedWriter bw=new BufferedWriter(fw);
    bw.write("Hello");
    bw.close();
    fw.close();
    Thread.sleep(1000);
    }
    catch(InterruptedException ex)
    {

    }
    }

    String s="",s1="";
    public void readFile(File f2) throws IOException
    {
    try
    {
    FileReader fr=new FileReader(f);
    BufferedReader br=new BufferedReader(fr);
    while((s=br.readLine())!=null)
    {
    s1=s1+s;
    }
    System.out.println(s1);
    br.close();
    Thread.sleep(1000);
    }
    catch(InterruptedException ex)
    {

    }
    }

    public void writeFile(File f2) {
    // TODO Auto-generated method stub

    }
    }
    class Fileexample1 extends Thread
    {
    FileWriteFileRead f1;
    File f;
    public Fileexample1(FileWriteFileRead f1)
    {
    this.f1=f1;
    }
    public void run()
    {
    try {
    f1.createFile(f);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    f1.writeFile(f);

    }

    }

    class Fileexample2 extends Thread
    {
    FileWriteFileRead f1;
    File f;
    public Fileexample2(FileWriteFileRead f1)
    {
    this.f1=f1;
    }
    public void run()
    {

    try {
    f1.createFile(f);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    f1.readFile(f);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }
    public class Assignment2 {

    public static void main(String args[])throws IOException
    {
    FileWriteFileRead obj=new FileWriteFileRead();

    Fileexample1 obj1=new Fileexample1(obj);
    obj1.start();
    Fileexample2 obj2=new Fileexample2(obj);
    obj2.start();
    }
    }

    ReplyDelete
Post a Comment