Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java create new thread

new Thread(() -> {
  // code goes here
}).start();
Comment

thread in java


public class MyThread extends Thread {
    private final String name;

    public MyThread(String name) {
        this.name = name;
    }

    public void run() {
        try {
            for (; ; ) {
                System.out.println(name);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            System.out.println("sleep interrupted");
        }
    }

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

Comment

thread in java

//if your project supports java8, you can implement with lambda function
//for android
new Thread(() -> {
        // do background stuff here
        runOnUiThread(()->{
            // OnPostExecute stuff here
        });
    }).start();
Comment

create thread java

class RunnableObject implements Runnable {
   private Thread t;
   private String threadName;
   
   RunnableObject( String name) {
      System.out.println("Creating " +  threadName );
      threadName = name;
      t = new Thread (this, threadName);
   }
   
   public void run() {
      System.out.println("Running " +  threadName );
   }
   
   public void start () {
      System.out.println("Starting " +  threadName );
      t.start ();
   }
}

public class TestThread {

   public static void main(String args[]) {
      RunnableObject R1 = new RunnableObject( "Thread-1");
      R1.start();
   }   
}
Comment

Java Thread Example Using the Thread Class

class SoftHuntThread
{    
public static void main(String argvs[])  
{     
Thread thread= new Thread("Softhunt Thread");   
thread.start();    
String string = thread.getName();  
System.out.println(string);  
}  
}
Comment

java thread

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

Java Thread

class Java_Thread extends Thread{  
public void run(){  
System.out.println("thread is running...");  
}  
public static void main(String args[]){  
Java_Thread thread =new Java_Thread ();  
thread.start();  
}  
}
Comment

create thread

new thread(r)     r = Runnable  
Comment

PREVIOUS NEXT
Code Example
Java :: java infinite for loop 
Java :: java abstract modifier 
Java :: permutation and combination program in java 
Java :: java swing draw centered text 
Java :: Android listen to network change 
Java :: scaletype android dynamic 
Java :: fibonacci of 6 
Java :: stream filter java 8 
Java :: djava days between two dates 
Java :: java list of a class has a string that is equal to 
Java :: Java if...else Statement 
Java :: Java void Keyword 
Java :: lambda function java 
Java :: Salary example in method in java 
Java :: java multi thread 
Java :: java parameterized constructor 
Java :: get image file path 
Java :: login.html 
Java :: java collectors mapping 
Java :: how to create a new imageview in android java 
Java :: hashmap java 
Java :: Java Thread Example Using the Thread Class 
Java :: android activity transition 
Java :: generics Interface in java 
Java :: super 
Java :: how to et curent directory in java 
Java :: l datetime anne month jour heure minute second in java 
Java :: android tab theme 
Java :: print all prime no java 
Java :: Longest decreasing subsequence in java 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =