Quantcast
Viewing latest article 9
Browse Latest Browse All 10

Java Program to Implement Threads

Java Program to Implement Threads

 

Image may be NSFW.
Clik here to view.
java Programming

 

Java Program to implement threads : In this post we learn how we can implement thread in Java Programming.

 

JAVA Code :

 

public class ThreadShowName implements  Runnable   {
    public static void main(String[] args) {
        Thread t1= new Thread(new ThreadShowName());
        Thread t2= new Thread(new ThreadShowName());
        Thread t3= new Thread(new ThreadShowName());
        Thread t4= new Thread(new ThreadShowName());

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }

    public void run()
    {
    int pause, count=0;
        for(int i=0;i<10;i++)
        { 
            try{
                System.out.println(Thread.currentThread(). getName() + " executing.......");
                pause=(int)(Math.random()*5000);
                Thread.sleep(pause);
                count++;
            }
            catch(InterruptedException ex)
        {
          System.out.println(ex);
            }
        }
    }
}

 

The post Java Program to Implement Threads appeared first on CodingTalks.


Viewing latest article 9
Browse Latest Browse All 10

Trending Articles