Java to create a thread

There are four ways to create threads:

1, inheritance Thread class (thread class in the true sense) is to achieve Runnable interface.

2, implement Runnable , and override which run method.

3, create a thread pool using Executor framework. Executor framework is to achieve juc provided in the thread pool.

4, to achieve callable interfaces , call override method

 

The calling thread's start (): start the thread; call the appropriate run () method

Inherited from the Thread class Thread class, you can directly call the start method to start the thread (using static can also share resources). A thread (objects) can only be performed once start (), and can not run Thread class that implements the object () to start a thread.

After the start method is called to implement Runnable interface class needs to use the Thread class package again. (Three Thread object wrapper class object, you realize the sharing of resources).

Then pay attention to the use of locks and synchronized using threads. (Multi-threaded access to shared resources security thread prone)


 

In general, the common is the second.

* Runnable interface has the following benefits:

* ① point to avoid the limitations of inheritance, a class can inherit multiple interfaces.

* ② suitable for resource sharing

 

1, inheritance Thread

// Create a subclass inherits the Thread 
class the DemoThread the extends Thread {
     public  void RUN () {
         // RUN 2. Thread class rewritable () method, a method to realize the function of the child thread to be completed 
        for ( int I . 1 =; I <= 10; I ++ ) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}


public class TestThread {

    public  static  void main (String [] args) {
         // 3. Create a subclass object of 
        the DemoThread = S new new the DemoThread ();
         // 4. calling thread start (): this thread is started; call the appropriate run () method 
        s.start ();
        
        // main thread continues the following 
        for ( int I =. 1; I <= 10; I ++ ) {
            System.out.println(i);
        }
    }
}
/ * Common method of Thread:
 * 1.start (): Start a thread and execute the corresponding run () method
 * 2.run (): Code sub-thread to be executed into the run () method
 * 3.currentThread (): static, calling the current thread
 * 4.getName (): Gets the name of this thread
 * 5.setName (): Set the name of this thread
 * 6.yield (): This method of releasing the calling thread currently executing the right cpu
 * 7.join (): calling thread is waiting to run under the thread after the completion, in order to continue with
 * 8.isAlive (): determine whether the current thread still alive
 * 9.sleep (long 1): explicitly allow the current thread to sleep for 1 millisecond
 * 10. The thread communication: wait () notify () notifyAll ()
 * 
 * Set thread priority
 * GetPriority (): Returns the thread priority value
 * SetPriority (int newPriority): to change the thread priority
 * */

 A multithreaded small demo:

/ * Create two sub-thread, so that one even-numbered output between 1-100, 1-100 between the other output odd * /

class Thread1 extends Thread{
    public void run(){
        for(int i=1;i<=100;i++){
            if(i%2==0)
                System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}

class Thread2 extends Thread{
    public void run(){
        for(int i=1;i<=100;i++){
            if(i%2!=0)
                System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}


public class TestThread {
    public static void main(String[] args) {
        Thread1 s1 = new Thread1();
        Thread2 s2 = new Thread2();
        
        s1.start();
        s2.start();
        
        //继承于Thread类的匿名类的对象
//        new Thread(){
//            public void run(){
//                for(int i=1;i<=100;i++){
//                    if(i%2==0)
//                        System.out.println(Thread.currentThread().getName()+":"+i);
//                }
//            }
//        }.start();
//
//        new Thread(){
//            public void run(){
//                for(int i=1;i<=100;i++){
//                    if(i%2!=0)
//                        System.out.println(Thread.currentThread().getName()+":"+i);
//                }
//            }
//        }.start();
    }
}
View Code

 

 2, by way of implement Runnable

/ * Create the even between the two sub-thread, output 1-100 * /

class Thread1 implements Runnable{
    public void run(){
        for(int i=1;i<=100;i++){
            if(i%2==0)
                System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}public class TestThread {
    public static void main(String[] args) {
        Thread1 s = new Thread1();
        
        // To start a thread, you must call the Start () 
        the Thread T1 = new new the Thread (S);
        t1.start();
        
        Thread t2 = new Thread(s);
        t2.start();
    }
}

 

 
 
Thread与Runnable
1、联系:public class Thread implements Runnable
2, what a good way? The way to achieve better way of inheritance
     1) avoids the limitations of Java's single inheritance
     2) If multiple threads to operate with a resource (or data), the use of more suitable way to achieve
 
 
 
Reference and recommendation:

Guess you like

Origin www.cnblogs.com/lisen10/p/10970045.html