Java multi-threaded test

introduction

As a Java learner, we all know that Java multi-threaded implementation There are four kinds as follows:

  • Thread class inheritance;
  • Implement Runnable;
  • Use ExecutorService, Callable, Future have realized returns the result of a multi-threaded;
  • Create a thread by thread pool.
    Two kinds of the foregoing may be attributed to a Class: None Return Value simple reason that, by rewriting the run method, the return value is run void embodiment, there is no way to return results.
    The latter two can be attributed to a Class: returns a value, through the Callable interface method call will be realized, the return value is Object, so the results can be returned in Object object.

    The first method: Thread class inheritance, class override run () method.

class A extends Thread{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第一个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i+1;
            System.out.println(i);
        }
    }
}
class B extends Thread{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第二个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i+1;
            System.out.println(i);
        }
    }
}
class C extends Thread{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第三个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i-1;
            System.out.println(i);
        }
    }
}

class D extends Thread{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第四个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i-1;
            System.out.println(i);
        }
    }
}
public class manu  {
    public static void main(String[] args) throws Exception {
        A a = new A();
        B b = new B();
        C c = new C();
        D d = new D();
        a.start();
        a.join();
        b.start();
        b.join();
        c.start();
        c.join();
        d.start();
    }
}

The second method: implement Runnable, and rewriting the interface run () method.

class A implements Runnable {
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第一个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i+1;
            System.out.println(i);
        }
    }
}

class B implements Runnable{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第二个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i+1;
            System.out.println(i);
        }
    }
}

class C implements Runnable{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第三个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i-1;
            System.out.println(i);
        }
    }
}

class D implements Runnable{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第四个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i-1;
            System.out.println(i);
        }
    }
}
public class manu  {
    public static void main(String[] args) throws Exception {
        A a = new A();
        B b = new B();
        C c = new C();
        D d = new D();
        Thread a1 = new Thread(a);
        Thread b1 = new Thread(b);
        Thread c1 = new Thread(c);
        Thread d1 = new Thread(d);
        a1.start();a1.join();
        b1.start();b1.join();
        c1.start();c1.join();
        d1.start();d1.join();
    }
}

The third method: Create Callable and Future interfaces used by Lambda expressions

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
class A  {
    private int i = 0;
    FutureTask task1 = new FutureTask(
    (Callable)()->{
        System.out.println("第一个进程:"); 
        for (int n =1;n<=10;n++) { 
            i=i+1; 
            System.out.println(i); 
        } 
        return 0; 
    } ); 
}
class B  {
    private int i = 0;
    FutureTask task2 = new FutureTask(
    (Callable)()->{
        System.out.println("第二个进程:"); 
        for (int n =1;n<=10;n++) { 
            i=i+1; 
            System.out.println(i); 
        } 
        return 0; 
    } ); 
}
class C  {
    private int i = 0;
    FutureTask task3 = new FutureTask(
    (Callable)()->{
        System.out.println("第三个进程:"); 
        for (int n =1;n<=10;n++) { 
            i=i+1; 
            System.out.println(i); 
        } 
        return 0; 
    } ); 
}
class D  {
    private int i = 0;
    FutureTask task4 = new FutureTask(
    (Callable)()->{
        System.out.println("第四个进程:"); 
        for (int n =1;n<=10;n++) { 
            i=i+1; 
            System.out.println(i); 
        } 
        return 0; 
    } ); 
}
public class manu {
    public static void main(String[] args) throws Exception {
        A a = new A(); 
        B b = new B(); 
        C c = new C(); 
        D d = new D(); 
        new Thread(a.task1).start(); 
        new Thread(a.task1).setPriority(Thread.MAX_PRIORITY); 
        new Thread(b.task2).start(); 
        new Thread(b.task2).setPriority(8); 
        new Thread(c.task3).start(); 
        new Thread(c.task3).setPriority(7);
        new Thread(d.task4).start(); 
        new Thread(d.task4).setPriority(Thread.MIN_PRIORITY); 
    } 
}

The fourth method: create a thread pool by

public class ThreadDemo05{
    private static int POOL_NUM = 10; //线程池数量 
    /**
     * @param args
     * @throws InterruptedException 
     */ 
    public static void main(String[] args) throws InterruptedException { 
        // TODO Auto-generated method stub 
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for(int i = 0; i<POOL_NUM; i++) { 
            RunnableThread thread = new RunnableThread();
            //Thread.sleep(1000); 
            executorService.execute(thread); 
        } 
        //关闭线程池 
        executorService.shutdown(); 
    } 
} 

class RunnableThread implements Runnable { 
    @Override 
    public void run() { 
        System.out.println("通过线程池方式创建的线程:" + Thread.currentThread().getName() + " "); 
    } 
}

Guess you like

Origin www.cnblogs.com/jiang4yu/p/11240517.html