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 :: creating a properties object using a file 
Java :: my canvas java 
Java :: set array length java 
Java :: mcq java 
Java :: string comparison using == in java 
Java :: java hashmap set value 
Java :: how to use another class in java inside of an action listenrer 
Java :: java to python 
Java :: java evaluate two expressions in if statemenmt 
Java :: android activity set action bar options 
Java :: in dom parser how to find processing instruction in java 
Java :: edit text on 2sec change andropid 
Java :: Does JVM create object of Main class (the class with main())? 
Java :: android pick up photo from local device 
Java :: get sha1 key from keystore 
Java :: key caracter from code java 
Java :: android java string animations 
Java :: close current file android studio shortct 
Java :: javacal 
Java :: error: package android.support.v4.content does not exist import android.support.v4.content.LocalBroadcastManager; 
Java :: How do I retrieve editors registered for a certain file extension in Eclipse? 
Java :: java write number with variable decimal 
Java :: Performance data in appium 
Java :: linearview 2 items next to each other 
Java :: java.lang.String cannot be cast to java.util.Map 
Java :: import classes from another project java 
Java :: call method without creating object java 
Java :: java jackson optional 
Java :: how to decode a ByteArray to Bitman in adroid 
Java :: turn on device location android programmatically 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =