Showing posts with label Java Thread Codes. Show all posts
Showing posts with label Java Thread Codes. Show all posts

TwoThreads example

class A extends Thread
{
public void run()
{
    for(int i=1;i<=10;i++)
    {
        System.out.print(i);
                try
                  {
                    Thread.sleep(500);
                }
                catch(InterruptedException e)
                {
                System.out.println("threadA interrupted " );
                }
    }
    System.out.println();
    System.out.println("Exiting threadA " );
}
}

class B extends Thread
{
public void run()
{
    for(char i=65;i<=74;i++)
    {
        System.out.print(i);
                try
                  {
                    Thread.sleep(500);
                }
                catch(InterruptedException e)
                {
                System.out.println("threadB interrupted " );
                }
    }
    System.out.println("Exiting threadB" );
}
}

class TwoThreads1A2B
{
    public static void main(String args[])
    {
        A a =new A();
        a.start();
        B b =new B();
        b.start();
        try
        {
        a.join();
        b.join();
        }
        catch(InterruptedException e)
        {
            System.out.println("Main thread interrupted " );
        }
        System.out.println("Exiting main thread ");
    }
}
READ MORE - TwoThreads example

Program to set thread priority

class A extends Thread
{
public void run()
{
    for(int i=1;i<=5;i++)
    {
        System.out.println("threadA : "+i);

    }
}
}
class B extends Thread
{
public void run()
{
    for(int i=1;i<=5;i++)
    {
        System.out.println("threadB : "+i);

    }
}
}

class ThreadPri
{
    public static void main(String args[])
    {
        A a =new A();
        B b =new B();
        a.setPriority(Thread.MIN_PRIORITY);
        b.setPriority(Thread.MIN_PRIORITY);
        a.start();
        b.start();

    }
}
READ MORE - Program to set thread priority

Program to demonstrate thread methods

 class A extends Thread
  {

    public void run()
    {
          for (int i=1; i<=5;i++)
               {
                   if (i==2)
                       yield();
                   System.out.println(this.getName()+" : "+ i);

               }

     System.out.println("End of child thread : "+this.getName());


    }
}


class B extends Thread
 {

   public void run()
   {
         for (int j=1; j<=5;j++)
              {
                  System.out.println(this.getName()+" : "+ j);


              }
    System.out.println("End of child thread : "+this.getName());


   }
}


class C extends Thread
 {

   public void run()
   {
       for (int k=1; k<=5;k++)
       {
                  System.out.println(this.getName()+" : "+ k);
                  try
                  {
                    sleep(500);
                }
                catch(InterruptedException e)
                {

                }

          }
    System.out.println("End of child thread : "+this.getName());


   }
}

class ThreadMethods
{
public static void main(String args[])
{
    A threadA=new A();
    B threadB=new B();
    C threadC=new C();

    threadA.setName("Thread A");
    threadB.setName("Thread B");
    threadC.setName("Thread C");


    System.out.println("Start Thread A");
    threadA.start();

    System.out.println("Start Thread B");
    threadB.start();

    System.out.println("Start Thread C");
    threadC.start();

    for (int k=1; k<=5;k++)
       {
                  System.out.println("Main Thread  : "+ k);
                  try
                  {
                    Thread.sleep(1000);
                }
                catch(InterruptedException e)
                {

                }

          }
    System.out.println("End of main thread!");
}
}
READ MORE - Program to demonstrate thread methods

Thread MAX_PRIORITY example

class A extends Thread
{
public void run()
{
    for(int i=1;i<=5;i++)
    {
        System.out.println("threadA : "+i);

    }
}
}
class B extends Thread
{
public void run()
{
    for(int i=1;i<=5;i++)
    {
        System.out.println("threadB : "+i);

    }
}
}

class ThreadMax
{
    public static void main(String args[])
    {
        A a =new A();
        B b =new B();
        b.setPriority(Thread.MAX_PRIORITY);
        a.run();
        b.run();

    }
}
READ MORE - Thread MAX_PRIORITY example

synchronization of threads

 class ThreadSafeCounter
{
   private int count = 0;  // The value of the counter.
   public void increment()
   {
       count = count + 1;
        try
        {
            Thread.sleep(500);
           }
           catch(InterruptedException e)
           {
        }
       System.out.println("Count : "+count);
   }

   }


class Ourthread extends Thread
{
    ThreadSafeCounter tsc;
    Ourthread(ThreadSafeCounter t)
    {
        tsc=t;
    }

    public void run()
    {
        synchronized(tsc)
        {
            tsc.increment();
        }

    }
}


class Synch3
{
public static void main(String args[])
{
ThreadSafeCounter tsc=new ThreadSafeCounter();
Ourthread t1=new Ourthread(tsc);
Ourthread t2=new Ourthread(tsc);

t1.start();
t2.start();

try
{
t1.join();
t2.join();

}
catch(InterruptedException e)
{
}
System.out.println("End of main thread");
}
}
READ MORE - synchronization of threads

Program to demonstrate synchronization in java

 class ThreadSafeCounter
{
   private int count = 0;  // The value of the counter.
   synchronized public void increment()
   {
       count = count + 1;
        try
        {
            Thread.sleep(500);
           }
           catch(InterruptedException e)
           {
        }
       System.out.println("Count : "+count);
   }

   }


class Ourthread extends Thread
{
    ThreadSafeCounter tsc;
    Ourthread(ThreadSafeCounter t)
    {
        tsc=t;
    }
    public void run()
    {
        tsc.increment();

    }
}


class Synch2
{
public static void main(String args[])
{
ThreadSafeCounter tsc=new ThreadSafeCounter();
Ourthread t1=new Ourthread(tsc);
Ourthread t2=new Ourthread(tsc);

t1.start();
t2.start();

try
{
t1.join();
t2.join();

}
catch(InterruptedException e)
{
}
System.out.println("End of main thread");
}
}
READ MORE - Program to demonstrate synchronization in java

program to suspend and resume thread

class ChildThread implements Runnable
{
    String name;
    Thread t;
    boolean suspendFlag;
    ChildThread(String threadname)
    {
        name=threadname;
        t=new Thread(this,name);
        System.out.println("New Thread : "+t);
        suspendFlag=false;
        t.start();
    }

    public void run()
    {
        try
        {
            for(int i=1;i<=10;i++)
            {
                System.out.println(name+" : "+i);
                Thread.sleep(200);
                synchronized(this)
                {
                    while(suspendFlag)
                    {
                        wait();
                    }
                }
            }
        }
        catch(InterruptedException e)
        {
            System.out.println(name+"interrupted");
        }
        System.out.println(name+" exiting");
    }

    void suspendThread()
    {
        suspendFlag=true;
    }

    synchronized void resumeThread()
    {
        suspendFlag=false;
        notify();
    }
}


class SuspendResume
{
    public static void main(String args[])
    {
        ChildThread t1=new ChildThread("One");
        ChildThread t2=new ChildThread("Two");
        try
        {
            Thread.sleep(500);
            System.out.println("Suspending thread one");
            t1.suspendThread();
            Thread.sleep(500);
            System.out.println("Resuming thread one");
            t1.resumeThread();
            System.out.println("Suspending thread two");
            t2.suspendThread();
            Thread.sleep(500);
            System.out.println("Resuming thread two");
            t2.resumeThread();

        }
        catch(InterruptedException e)
        {
            System.out.println("Main thread interrupted ");
        }
        try
        {
            System.out.println("waiting for threads to finish");
            t1.t.join();
            t2.t.join();
        }
        catch(InterruptedException e)
        {
            System.out.println("Main thread interrupted");
        }
        System.out.println("Main thread exiting ");
    }
}
READ MORE - program to suspend and resume thread

Thread start() and sleep() example

class A extends Thread
{
public void start()
{
    for(int i=1;i<=5;i++)
    {
        System.out.println("threadA : "+i);
        try
        {
        Thread.sleep(1000);
        }
        catch(InterruptedException e)
        {
            System.out.println("Thread Interrupted");
        }

    }
}
}
class StartThread
{
    public static void main(String args[])
    {
        A a =new A();
        a.start();
        for(int i=1;i<=5;i++)
        {
            System.out.println("Main thread : "+i);
            try
            {
                Thread.sleep(500);
            }
            catch(InterruptedException e)
            {
                System.out.println("Thread Interrupted");
            }
        }

    }
}
READ MORE - Thread start() and sleep() example

Implementing Runnable interface

class NamedRunnable implements Runnable
{
   private String name;  // The name of this thread.
   public NamedRunnable(String name)
   {  // Constructor gives name to object.
      this.name = name;
   }


   public void run()
   {  // The run method prints a message to standard output.
        for (int i=1; i<=10;i++)
        {
            System.out.println("Greetings from : "+name);
        }
        System.out.println("End of child thread!");
   }
}

class RunnableTest2
{
    public static void main(String args[])
    {
        NamedRunnable greetings = new NamedRunnable("Child Thread");
        Thread greetingsThread = new Thread(greetings);
        greetingsThread.start();
        for (int i=1; i<=10;i++)
                {
                    System.out.println("Main Thread ");
        }
        System.out.println("End of main thread!");
    }

}
READ MORE - Implementing Runnable interface

Checking thread priority

 class A extends Thread
  {

    public void run()
    {
        for (int i=1; i<=5;i++)
        {
               System.out.println(this.getName()+" : "+ i);

        }
         System.out.println("End of child thread : "+this.getName());

    }
}


class B extends Thread
 {

   public void run()
   {
        for (int j=1; j<=5;j++)
        {
             System.out.println(this.getName()+" : "+ j);
        }
        System.out.println("End of child thread : "+this.getName());

    }
}


class C extends Thread
 {

   public void run()
   {
        for (int k=1; k<=5;k++)
        {
             System.out.println(this.getName()+" : "+ k);
         }

    System.out.println("End of child thread : "+this.getName());
   }
}


class PriorityDemo
{
public static void main(String args[])
{
    A threadA=new A();
    B threadB=new B();
    C threadC=new C();

    threadA.setName("Thread A");
    threadB.setName("Thread B");
    threadC.setName("Thread C");


    threadA.setPriority(Thread.MAX_PRIORITY);
    threadB.setPriority(Thread.MIN_PRIORITY);
    threadC.setPriority(Thread.NORM_PRIORITY);


    System.out.println("Start Thread B");
    threadB.start();

    System.out.println("Start Thread C");
    threadC.start();

    System.out.println("Start Thread A");
    threadA.start();



    try
    {

    threadA.join();
    threadB.join();
    threadC.join();
    }
    catch(InterruptedException e)
    {
    System.out.println("Main Thread  interrupted");
    }

    System.out.println("End of main thread!");

}
}
READ MORE - Checking thread priority

Multithread test example in java

 class NamedThread extends Thread
 {
   private String name;  // The name of this thread.
   public NamedThread(String name)
   {  // Constructor gives name to thread.
      super(name);
      this.name = name;
      System.out.println(this);
      start();
   }
   public void run()
   {  // The run method prints a message to standard output.
         for (int i=1; i<=10;i++)
              {
                  System.out.println("Greetings from : "+name);
              }
    System.out.println("End of child thread!");


   }
}

class MultiThreadsTest
{
public static void main(String args[])
{
    new NamedThread("Child Thread 1");
    new NamedThread("Child Thread 2");
    new NamedThread("Child Thread 3");

    for (int i=1; i<=10;i++)
    {
        System.out.println("Main Thread ");
    }
    System.out.println("End of main thread!");
}
}
READ MORE - Multithread test example in java

Simple thread example

class A extends Thread
{
public void run()
{
    for(int i=1;i<=5;i++)
    {
        System.out.println("threadA : "+i);

    }
}
}
class B implements Runnable
{
public void run()
{
    for(int i=1;i<=5;i++)
    {
        System.out.println("threadB : "+i);

    }
}
}

class MultiCheck
{
    public static void main(String args[])
    {
        A a =new A();
        a.start();
        B b =new B();
        b.start();

    }
}
READ MORE - Simple thread example

Simple Thread Example

class ThreadDemo
{
public static void main(String main[])
{
    Thread t=Thread.currentThread();
    System.out.println("Current Thread : "+t);
    //change the name of the thread
    t.setName("My thread");
    System.out.println("Current Thread after name changed : "+t);
    System.out.println("Number of active threads : "+t.activeCount());


}
}
READ MORE - Simple Thread Example

Thread test example

class A extends Thread
{
public void run()
{
    System.out.println("Begin ");
    start();
    System.out.println("End ");


}

public void start()
{

}
}


class ThreadTest
{
    public static void main(String args[])
    {
    A a =new A();
    a.start();
    }
}
READ MORE - Thread test example

thread with InterruptedException

class A extends Thread
{
public void run()
{
    for(int i=1;i<=5;i++)
    {
        System.out.println("threadA : "+i);
        Thread.sleep(1000);
    }
}
}
class ThreadClass
{
    public static void main(String args[])
    {
        A a =new A();
        a.start();
    }
}

/*
the program will not be compiled because Thread.sleep throws InterruptedException which must be caught */
READ MORE - thread with InterruptedException

Runnable example in Java

class NamedRunnable implements Runnable
{
   private String name;  // The name of this thread.
   public NamedRunnable(String name)
   {  // Constructor gives name to object.
   this.name=name;
   Thread greetingsThread = new Thread(this,this.name);
   System.out.println(greetingsThread);
   greetingsThread.start();
   }


   public void run()
   {  // The run method prints a message to standard output.
        for (int i=1; i<=10;i++)
        {
            System.out.println("Greetings from : "+name);
        }
        System.out.println("End of child thread!");
   }
}

class RunnableTest3
{
    public static void main(String args[])
    {
        NamedRunnable greetings = new NamedRunnable("ChildThread");

        for (int i=1; i<=10;i++)
                {
                    System.out.println("Main Thread ");
        }
        System.out.println("End of main thread!");
    }

}
READ MORE - Runnable example in Java

Checking is thread Alive

 class A extends Thread
  {

    public void run()
    {
          for (int i=1; i<=5;i++)
               {
                   if (i==2)
                       yield();
                   System.out.println(this.getName()+" : "+ i);

               }

     System.out.println("End of child thread : "+this.getName());


    }
}


class B extends Thread
 {

   public void run()
   {
         for (int j=1; j<=5;j++)
              {
                  System.out.println(this.getName()+" : "+ j);


              }
    System.out.println("End of child thread : "+this.getName());


   }
}


class C extends Thread
 {

   public void run()
   {
       for (int k=1; k<=5;k++)
       {
                  System.out.println(this.getName()+" : "+ k);
                  try
                  {
                    sleep(500);
                }
                catch(InterruptedException e)
                {

                }

          }
    System.out.println("End of child thread : "+this.getName());


   }
}

class IsaliveJoinDemo
{
public static void main(String args[])
{
    A threadA=new A();
    B threadB=new B();
    C threadC=new C();

    threadA.setName("Thread A");
    threadB.setName("Thread B");
    threadC.setName("Thread C");


    System.out.println("Start Thread A");
    threadA.start();
    System.out.println("Thread A is alive : "+threadA.isAlive());

    System.out.println("Start Thread B");
    threadB.start();
    System.out.println("Thread B is alive : "+threadB.isAlive());

    System.out.println("Start Thread C");
    threadC.start();
    System.out.println("Thread C is alive : "+threadC.isAlive());

    for (int k=1; k<=5;k++)
    {
        System.out.println("Main Thread  : "+ k);
    }

    try
    {

    threadA.join();
    threadB.join();
    threadC.join();
    }
    catch(InterruptedException e)
    {
    System.out.println("Main Thread  interrupted");
    }

    System.out.println("End of main thread!");

    System.out.println("Thread A is alive : "+threadA.isAlive());
    System.out.println("Thread B is alive : "+threadB.isAlive());
    System.out.println("Thread C is alive : "+threadC.isAlive());
}
}
READ MORE - Checking is thread Alive

Thread in java

 class NamedThread extends Thread
 {
   private String name;  // The name of this thread.
   NamedThread(String name)
   {  // Constructor gives name to thread.
      super(name);
      this.name = name;
      System.out.println(this);
      start();
   }
   public void run()
   {  // The run method prints a message to standard output.
         for (int i=1; i<=10;i++)
              {
                  System.out.println("Greetings from : "+name);
              }
    System.out.println("End of child thread!");


   }
}

class ExtendThread
{
public static void main(String args[])
{
    new NamedThread("Child Thread");



    System.out.println("Thread has been started.");
    for (int i=1; i<=10;i++)
    {
        System.out.println("Main Thread ");
    }
    System.out.println("End of main thread!");
}
}

READ MORE - Thread in java

Thread example in java

 class A implements Runnable
  {
    String name;
    Thread ThreadA;
    A(String Threadname)
    {
        name=Threadname;
        ThreadA=new Thread(this,name);
        System.out.println("New Thread : "+ThreadA);
        ThreadA.start();
    }
    public void run()
    {
          for (int i=1; i<=5;i++)
               {

                   System.out.println(ThreadA.getName()+" : "+ i);
                try
                  {
                    ThreadA.sleep(500);
                }
                catch(InterruptedException e)
                {

                }
               }

     System.out.println("End of child Thread : "+ThreadA.getName());


    }
}


class B implements Runnable
 {
    String name;
    Thread ThreadB;
    B(String Threadname)
    {
        name=Threadname;
        ThreadB=new Thread(this,name);
        System.out.println("New Thread : "+ThreadB);
        ThreadB.start();
    }
   public void run()
   {
         for (int j=1; j<=5;j++)
              {
                  System.out.println(ThreadB.getName()+" : "+ j);
                try
                  {
                    ThreadB.sleep(500);
                }
                catch(InterruptedException e)
                {

                }

              }
    System.out.println("End of child Thread : "+ThreadB.getName());


   }
}


class C implements Runnable
 {

    String name;
    Thread ThreadC;
    C(String Threadname)
    {
        name=Threadname;
        ThreadC=new Thread(this,name);
        System.out.println("New Thread : "+ThreadC);
        ThreadC.start();
    }
   public void run()
   {
       for (int k=1; k<=5;k++)
       {
                  System.out.println(ThreadC.getName()+" : "+ k);
                  try
                  {
                    ThreadC.sleep(500);
                }
                catch(InterruptedException e)
                {

                }

          }
    System.out.println("End of child Thread : "+ThreadC.getName());


   }
}

class IsaliveJoinRunnable
{
public static void main(String args[])
{
    A a=new A("A");
    B b=new B("B");
    C c=new C("C");


    System.out.println("Thread A is alive : "+a.ThreadA.isAlive());

    System.out.println("Thread B is alive : "+b.ThreadB.isAlive());

    System.out.println("Thread C is alive : "+c.ThreadC.isAlive());

    for (int k=1; k<=5;k++)
    {
        System.out.println("Main Thread  : "+ k);
                try
                  {
                    Thread.sleep(500);
                }
                catch(InterruptedException e)
                {

                }
    }

    try
    {

    a.ThreadA.join();
    b.ThreadB.join();
    c.ThreadC.join();
    }
    catch(InterruptedException e)
    {
    System.out.println("Main Thread  interrupted");
    }

    System.out.println("End of main Thread!");

    System.out.println("Thread A is alive : "+a.ThreadA.isAlive());
    System.out.println("Thread B is alive : "+b.ThreadB.isAlive());
    System.out.println("Thread C is alive : "+c.ThreadC.isAlive());
}
}
READ MORE - Thread example in java

Extends Thread example

 class NamedThread extends Thread
 {
   private String name;  // The name of this thread.
   public NamedThread(String name)
   {  // Constructor gives name to thread.
      this.name = name;
   }
   public void run()
   {  // The run method prints a message to standard output.
         for (int i=1; i<=10;i++)
              {
                  System.out.println("Greetings from : "+name);
              }
    System.out.println("End of child thread!");


   }
}

class ExtendTest
{
public static void main(String args[])
{
    NamedThread greetings = new NamedThread("Child Thread");
    greetings.start();
    System.out.println("Thread has been started.");
    for (int i=1; i<=10;i++)
    {
        System.out.println("Main Thread ");
    }
    System.out.println("End of main thread!");
}
}
READ MORE - Extends Thread example

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|