Friday, 20 January 2017

Java Tutorial: Java Threads (How to execute a single task by multiple threads)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=nGPwkBeuFqY&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Java Threads (How to execute a single task by multiple threads) 
MultitaskingThread.java
/*
 * Program of performing single task by multiple threads
 */
public class MultitaskingThread extends Thread
{

    public static void main(String args[]) throws InterruptedException
    {
        MultitaskingThread t1 = new MultitaskingThread();
        MultitaskingThread t2 = new MultitaskingThread();
        MultitaskingThread t3 = new MultitaskingThread();

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

    /*
     * If you have to perform single task by many threads, have only
     * one run() method.
     */
    public void run()
    {
        System.out.println("task 1" + " Run by = "
                + Thread.currentThread().getName());
    }
}
Output
task 1 Run by = Thread-0
task 1 Run by = Thread-2
task 1 Run by = Thread-1
MultitaskingRunnable.java
/*
 * Program of performing single task by multiple threads
 */
public class MultitaskingRunnable implements Runnable
{

    public static void main(String[] args)
    {
        MultitaskingRunnable multitaskingRunnable = new MultitaskingRunnable();
        Thread t1 = new Thread(multitaskingRunnable);
        Thread t2 = new Thread(multitaskingRunnable);

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

    }

    public void run()
    {
        System.out.println("task 1" + " Run by = "
                + Thread.currentThread().getName());
    }

}
Output
task 1 Run by = Thread-0
task 1 Run by = Thread-1

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ThreadDemo_SingleTask_MT_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ThreadDemo_SingleTask_MT_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4a03b5014bb513b61dfa414428ea9747b1421899/BasicJava/ThreadDemo_SingleTask_MT_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge