Example: A simple Java multithreading program with two threads.
class MyThread implements Runnable {
   public void run() {
      for (int i = 1; i <= 10; i++) {
         System.out.println("Thread " + Thread.currentThread().getId() + ": " + i);
      }
   }
}

public class Main {
   public static void main(String[] args) {
      Thread t1 = new Thread(new MyThread());
      Thread t2 = new Thread(new MyThread());
      t1.start();
      t2.start();
   }
}

Explaination:
Here, we define a class MyThread that implements the Runnable interface, which defines a single run() method. Inside the run() method, we simply print out the thread ID and a number from 1 to 10.

In the Main class, we create two instances of Thread, passing in an instance of MyThread as the argument. We then call the start() method on each thread, which starts the thread and calls the run() method of the MyThread instance on a separate thread of execution.

When we run this program, we should see two threads running simultaneously, printing out their IDs and the numbers from 1 to 10 in an unpredictable order.

Output:
Thread 11: 1
Thread 11: 2
Thread 11: 3
Thread 11: 4
Thread 11: 5
Thread 11: 6
Thread 11: 7
Thread 11: 8
Thread 11: 9
Thread 11: 10
Thread 12: 1
Thread 12: 2
Thread 12: 3
Thread 12: 4
Thread 12: 5
Thread 12: 6
Thread 12: 7
Thread 12: 8
Thread 12: 9
Thread 12: 10

-------------  OR  -------------

class MultithreadingExample {
   public static void main(String[] args) {
      Thread thread1 = new Thread(new Runnable() {
         public void run() {
            for (int i = 1; i <= 5; i++) {
               System.out.println("Thread 1: " + i);
               try {
                  Thread.sleep(1000);
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }
            }
         }
      });

      Thread thread2 = new Thread(new Runnable() {
         public void run() {
            for (int i = 1; i <= 5; i++) {
               System.out.println("Thread 2: " + i);
               try {
                  Thread.sleep(1000);
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }
            }
         }
      });

      thread1.start();
      thread2.start();
   }
}

NB:This program creates two threads, each of which prints out numbers from 1 to 5 with a delay of 1 second between each number. The Thread class is used to create and start the threads. The Runnable interface is implemented by defining an anonymous inner class that overrides the run() method to define the thread's behavior.

Output:

Thread 1: 1
Thread 2: 1
Thread 2: 2
Thread 1: 2
Thread 2: 3
Thread 1: 3
Thread 2: 4
Thread 1: 4
Thread 2: 5
Thread 1: 5
Example: A simple Java multithreading program with two threads.
public class SimpleThreadExample {
    
    public static void main(String[] args) {
        
        // create two threads
        Thread t1 = new Thread(new MyRunnable("Thread 1"));
        Thread t2 = new Thread(new MyRunnable("Thread 2"));
        
        // start both threads
        t1.start();
        t2.start();
    }
}

class MyRunnable implements Runnable {
    
    private String name;
    
    public MyRunnable(String name) {
        this.name = name;
    }
    
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(name + " is running...");
            try {
                Thread.sleep(1000); // sleep for 1 second
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name + " has finished.");
    }
}


Explaination: 
We create a class MyRunnable that implements the Runnable interface. This interface defines a single method run() that will be executed when the thread starts. In the run() method, we simply print out a message indicating that the thread is running, and then sleep for 1 second using Thread.sleep().

In the main() method, we create two Thread objects, passing in an instance of MyRunnable and a name for each thread. We then start both threads using the start() method. When the threads start running, they will execute the run() method of the MyRunnable class, printing out their name and sleeping for 1 second.

Here, the two threads are running concurrently, with each thread printing out its name and sleeping for 1 second before printing again. The output shows that the threads are running in parallel, with no predictable ordering between them.

Output:

Thread 2 is running...
Thread 1 is running...
Thread 2 is running...
Thread 1 is running...
Thread 2 is running...
Thread 1 is running...
Thread 2 is running...
Thread 1 is running...
Thread 2 is running...
Thread 1 is running...
Thread 2 has finished.
Thread 1 has finished.

Here, we create a class MyRunnable that implements the Runnable interface. This interface defines a single method run() that will be executed when the thread starts. In the run() method, we simply print out a message indicating that the thread is running, and then sleep for 1 second using Thread.sleep().

In the main() method, we create two Thread objects, passing in an instance of MyRunnable and a name for each thread. We then start both threads using the start() method. When the threads start running, they will execute the run() method of the MyRunnable class, printing out their name and sleeping for 1 second.

Loading

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.