java thread code example

Example 1: thread sleep java

package com.journaldev.threads;

public class ThreadSleep {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread.sleep(2000);
        System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));
        
    }

}

Example 2: thread join java

class ThreadJoining extends Thread 
{ 
    @Override
    public void run() 
    { 
        for (int i = 0; i < 2; i++) 
        { 
            try
            { 
                Thread.sleep(500); 
                System.out.println("Current Thread: "
                        + Thread.currentThread().getName()); 
            } 
  
            catch(Exception ex) 
            { 
                System.out.println("Exception has" + 
                                " been caught" + ex); 
            } 
            System.out.println(i); 
        } 
    } 
} 
  
class GFG 
{ 
    public static void main (String[] args) 
    { 
        ThreadJoining t1 = new ThreadJoining(); 
        ThreadJoining t2 = new ThreadJoining(); 
        ThreadJoining t3 = new ThreadJoining(); 
        t1.start(); 
        try
        { 
            System.out.println("Current Thread: "
                  + Thread.currentThread().getName()); 
            t1.join(); 
        } 
        catch(Exception ex) 
        { 
            System.out.println("Exception has " + 
                                "been caught" + ex); 
        } 
        t2.start(); 
        try
        { 
            System.out.println("Current Thread: "
                 + Thread.currentThread().getName()); 
            t2.join(); 
        } 
        catch(Exception ex) 
        { 
            System.out.println("Exception has been" + 
                                    " caught" + ex); 
        } 
        t3.start(); 
    } 
}

Example 3: java thread

public class ClassName extends Thread{
  public void run(){
    super.run();
    //Your Code
  }
}

public class Main{
  public static void main(String[] args){
    ClassName thread = new ClassName();
    thread.start();
  }
}

Example 4: java thread

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

Example 5: creating thread in java example

class Multi3 implements Runnable{  
public void run(){  
System.out.println("thread is running...");  
}  
  
public static void main(String args[]){  
Multi3 m1=new Multi3();  
Thread t1 =new Thread(m1);  
t1.start();  
 }  
}

Example 6: threads java

// Copy and test
// They run simultaneously

public static void main(String[] args) {
  		// How to create a thread
        Thread thread = new Thread(new Runnable() {
            @Override
			// Loop running in thread
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println("Printing plus " + i + " in a worker thread.");
                    try {
                        Thread.sleep(1000);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
  		// Loop running in main thread 
        for (int j = 0; j < 20 ; j++) {
            System.out.println("Printing plus " + j + " in a main thread.");
            try {
                Thread.sleep(900);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        }

Tags:

Java Example