PDF download Download Article PDF download Download Article

This wikiHow will teach you how to run multiple threads in Java. You'll want to run multiple threads to create a program that processes multiple actions at once; the more CPU your computer has, the more processes it can run concurrently.

  1. public void run( )
    
    • This code provides a beginning point for your multiple threads to run.
  2. Thread(Runnable threadObj, String threadName);
    
    • 'threadObj' is the class that starts the runnable thread and 'threadName' is the name of the thread.
    Advertisement
  3. void start();
    
    • Use this code after you've fleshed out a thread object and this code will start it.
    • Your finished code could look like this
      class RunnableDemo implements Runnable {
         private Thread t;
         private String threadName;
         
         RunnableDemo( String name) {
            threadName = name;
            System.out.println("Creating " +  threadName );
         }
         
         public void run() {
            System.out.println("Running " +  threadName );
            try {
               for(int i = 4; i > 0; i--) {
                  System.out.println("Thread: " + threadName + ", " + i);
                  // Let the thread sleep for a while.
                  Thread.sleep(50);
               }
            } catch (InterruptedException e) {
               System.out.println("Thread " +  threadName + " interrupted.");
            }
            System.out.println("Thread " +  threadName + " exiting.");
         }
         
         public void start () {
            System.out.println("Starting " +  threadName );
            if (t == null) {
               t = new Thread (this, threadName);
               t.start ();
            }
         }
      }
      
      public class TestThread {
      
         public static void main(String args[]) {
            RunnableDemo R1 = new RunnableDemo( "Thread-1");
            R1.start();
            
            RunnableDemo R2 = new RunnableDemo( "Thread-2");
            R2.start();
         }   
      }
      
  4. If you used the coding from the example, the output should read
    Creating Thread-1
    Starting Thread-1
    Creating Thread-2
    Starting Thread-2
    Running Thread-1
    Thread: Thread-1, 4
    Running Thread-2
    Thread: Thread-2, 4
    Thread: Thread-1, 3
    Thread: Thread-2, 3
    Thread: Thread-1, 2
    Thread: Thread-2, 2
    Thread: Thread-1, 1
    Thread: Thread-2, 1
    Thread Thread-1 exiting.
    Thread Thread-2 exiting.
    
  5. Advertisement


Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!

About This Article

Darlene Antonelli, MA
Written by:
wikiHow Technology Writer
This article was co-authored by wikiHow staff writer, Darlene Antonelli, MA. Darlene has been writing and editing tech content at wikiHow since 2019. She previously worked for AppleCare, served as a writing tutor, volunteered in IT at an animal rescue, and taught as an adjunct professor for EN101 and EN102. Darlene has completed Coursera courses on technology, writing, and language. She holds both a BA (2011) and an MA (2012) from Rowan University in Writing, with a focus on workplace communication. With her extensive experience, academic background, and ongoing learning, Darlene has become the go-to grammar expert for her friends and family, as well as a skilled wordsmith for anyone in need. This article has been viewed 17,655 times.
How helpful is this?
Co-authors: 3
Updated: March 1, 2021
Views: 17,655
Categories: Java
Article SummaryX

1. Enter public void run ( ) into your code.
2. Use Thread(runnable threadObj, String threadName); in your code.
3. Enter void start (); in your code.
4. Execute your code.

Did this summary help you?

Thanks to all authors for creating a page that has been read 17,655 times.

Is this article up to date?

Advertisement