Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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

PREVIOUS NEXT
Code Example
Java :: Java Hasmap Remove Elements 
Java :: insert node at end of doubly linked list 
Java :: how to declare a interface in java 
Java :: java first index of an arraylist 
Java :: Error: Could not find or load main class Hello Caused by: java.lang.ClassNotFoundException: Hello studio visual code 
Java :: list of list in java 
Java :: spring boot prerequisites 
Java :: object in java 
Java :: java hashmap replace 
Java :: calculating the percentile in java 
Java :: android studio how to call api 
Java :: java check if array element is null 
Java :: " meaning in java 
Java :: jenkins password decrypt online 
Java :: androi amterila inpout dialgue to get inouti diaogue 
Java :: how to change state of a Switch programmatically andoir dstudio 
Java :: print 1 to 10 using for loop in java 
Java :: map indexof java 
Java :: how to extract word from string in java 
Java :: list in list 
Java :: XmlRootElement Object to String 
Java :: javacal 
Java :: Get directory in android java 
Java :: viewresolver in spring boot 
Java :: about action Listioner in java Swing 
Java :: gradle project load test data json file with jackson 
Java :: java reverse a word 
Java :: firebase persistence enable android 
Java :: Java Create a FileOutputStream 
Java :: BasicAWSCredentials 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =