Google+ Followers

Connect

Get Updates Via Email

Enter your email address:

Delivered by FeedBurner

Sunday, 10 November 2013

Java Program: Multi Thread optimum way utilize CPU time using Thread(Runnable object, String thread_name)

Program

import java.util.logging.Level;
import java.util.logging.Logger;

class ThreadRunnnable {
    public static void main(String[] args)
    {
       
       new ThreadRun();
       new ThreadRunn();
       System.out.println("Main Thread Starts");
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }      
     
       
    }
}
class ThreadRun implements Runnable
{
    ThreadRun()
    {
       
        Thread t=new Thread(this,"Thread1");
        t.start();
        System.out.println("Thread 1");
       
    }
    @Override
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
   
    }  
               
}

class ThreadRunn implements Runnable
{
   
    ThreadRunn()
    {
       
        Thread t=new Thread(this,"Thread1");
        t.start();
        System.out.println("Thread 2");
       
    }
    @Override
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
               
    }  
               
}


Output

Thread 1
Thread 2
Main Thread Starts
Main Thread 1
Thread 2 1
Thread 1 1
Thread 2 2
Thread 2 3
Thread 2 4
Main Thread 2
Thread 2 5
Thread 1 2
Main Thread 3
Main Thread 4
Thread 1 3
Main Thread 5
Thread 1 4
Thread 1 5

Output of this program not unique it always random, because it depends on thread sleep method due to multiple threads, one thread waits and other threads are executing that's why java multi threads utilize the cpu time in optimum way

Java Program: Thread using Runnable interface program

Program

import java.util.logging.Level;
import java.util.logging.Logger;

class ThreadRunnnable {
    public static void main(String[] args)
    {      
       new ThreadRun();
       new ThreadRunn();
       System.out.println("Main Thread Starts");
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }      
     
    }
}
class ThreadRun implements Runnable
{
    ThreadRun()
    {      
        Thread t=new Thread();
        t.start();
        System.out.println("Thread 1");
        run();
    }
    @Override
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }  
    }                
}
class ThreadRunn implements Runnable
{  
    ThreadRunn()
    {      
        Thread t=new Thread();
        t.start();
        System.out.println("Thread 2");
        run();
    }
    @Override
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
             
    }  
}

Output

Thread 1
1
2
3
4
5
Thread 2
1
2
3
4
5
Main Thread Starts
1
2
3
4

5

Java Program: Thread in swing implements Runnable interface and SwingUtilities

Program
import javax.swing.*;
public class SwingThread extends JFrame {
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        final JFrame f = new JFrame("Swing using Thread Frame 1");
        f.setSize(600, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
     
        final JFrame fr = new JFrame("Swing using Thread Frame 2");
        fr.setSize(600, 600);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setVisible(true);
      }
    });
    }
 

}

Output:


Java Program: Simple Thread Example

public class threadexample {
    public static void main(String args[])
    {
        Thread t=Thread.currentThread();
        System.out.println("Current Thread: "+t);
        t.setName("Thread Example");
        System.out.println("Current Thread Name changed as: "+t);
    }

}

output

Java Program: Swap two numbers without temp variable

import java.io.*;
public class Swapwithouttemp {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter first number");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int x=Integer.parseInt(br.readLine());    
        System.out.println("Enter second number");
        BufferedReader brr=new BufferedReader(new InputStreamReader(System.in));
        int y=Integer.parseInt(brr.readLine());        
        new swap(x,y);
    }
}
class swap
{  
    swap(int a, int b) throws IOException          
    {
        System.out.println("Before swapped values a= "+a+"\tb= "+b );
        a=a+b;
        b=a-b;
        a=a-b;
        System.out.println("After swapped values a= "+a+"\tb= "+b );
    }
}

Java Program: User defined input using BufferedReader, Checking Employee Age

import java.io.*;
public class EmployeeAgeCheck {
    public static void main(String[] args) throws IOException
    {
        EmployeeData e=new EmployeeData();
        e.getdetails();
        e.checkage();      
    }
}
class EmployeeData
{
    private String name;
    private int age;

    public void getdetails() throws java.io.IOException
    {
        // user defined input at runtime
        BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter name");
        name=buf.readLine();
        System.out.println("Enter Age");
        age=Integer.parseInt(buf.readLine());
    }
    public void checkage()
    {
        if(age<=30)
        {
            System.out.println(name+ " ,You're Junior Employee");
        }
        else
        {
            System.out.println(name+ " ,You're Senior Employee");
        }
    }
}

Java Program : Default and Parameterized Constructor

public class constructorexample {
    public static void main(String[] args)
    {
        personcon kumaran=new personcon();
        kumaran.display();
        personcon jannu=new personcon("Jannu",22);
        jannu.display();
    }
}

class personcon
{
    private String name;
    private int age;
    //default constructor
    personcon()
    {
        name="kumaran";
        age=23;
    }
   //parameterized constructor
    personcon(String n,int a)          
    {
        name=n;
        age=a;
    }
    public void display()
    {
        System.out.println("Name: "+name+"\t Age: "+age);
    }
}