Java线程基础实例

时间:2022-05-23 09:35:01
  • 概述
    Java线程是一个在实战开发中经常使用的基础功能,而在Java中线程相关的类在java.lang和java.util.concurrent里

  • Thread

package thread.base;

/**
 * User: likang
 * Date: 16/8/14
 * Time: 下午4:27
 */
public class TestThread extends Thread {
    private Integer index;
    private String name;

    public TestThread(String name, Integer index) {
        this.name = name;
        this.index = index;
    }

    public void run() {
        try {
            Thread.sleep(1000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name + "Thread index:" + index);
    }
}
  • Runnable
package thread.base;

/**
 * User: likang
 * Date: 16/8/14
 * Time: 下午4:32
 */
public class TestRunnable implements Runnable {
    private Integer index;
    private String name;

    public TestRunnable(String name, Integer index) {
        this.name = name;
        this.index = index;
    }

    public void run() {
        try {
            Thread.sleep(1000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name + "Runnable index:" + index);
    }
}
  • Callable
package thread.base;

import java.util.concurrent.Callable;

/**
 * User: likang
 * Date: 16/8/14
 * Time: 下午4:35
 */
public class TestCallable implements Callable<String> {
    private Integer index;
    private String name;

    public TestCallable(String name, Integer index) {
        this.name = name;
        this.index = index;
    }

    @Override
    public String call() throws Exception {
        try {
            Thread.sleep(1000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return name + "Callable index:" + index;
    }
}
  • 实例测试
import thread.base.TestCallable;
import thread.base.TestRunnable;
import thread.base.TestThread;

import java.util.concurrent.*;

/**
 * User: likang
 * Date: 16/8/14
 * Time: 下午4:26
 */
public class ThreadTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("测试第一部分 Thread");
        for (Integer i = 0; i < 5; i++) {
            TestThread testThread = new TestThread("第一部分 ", i);
            testThread.start();
        }
        System.out.println("测试第二部分 Runnable+Thread");
        for (Integer i = 0; i < 5; i++) {
            TestRunnable testRunnable = new TestRunnable("第二部分 ", i);
            new Thread(testRunnable).start();
        }
        System.out.println("测试第三部分 Callable+Thread");
        for (Integer i = 0; i < 5; i++) {
            TestCallable testCallable = new TestCallable("第三部分 ", i);
            FutureTask<String> futureTask = new FutureTask<String>(testCallable);
            new Thread(futureTask).start();

            String result = futureTask.get();
            System.out.println(result);
        }
        System.out.println("测试第四部分 Runnable+CachedThreadPool");
        for (Integer i = 0; i < 5; i++) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            TestRunnable testRunnable = new TestRunnable("第四部分 ", i);
            executorService.submit(testRunnable);
        }
        System.out.println("测试第五部分 Runnable+CachedThreadPool");
        for (Integer i = 0; i < 5; i++) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            TestCallable testCallable = new TestCallable("第五部分 ", i);
            Future<String> future = executorService.submit(testCallable);
            System.out.println(future.get());
        }
        System.out.println("测试第六部分 Callable+CompletionService+CachedThreadPool");
        ExecutorService executorService = Executors.newCachedThreadPool();
        CompletionService<String> completionService = new ExecutorCompletionService<String>(executorService);
        for (Integer i = 0; i < 5; i++) {
            TestCallable testCallable = new TestCallable("第六部分 ", i);
            completionService.submit(testCallable);
        }
        for (Integer i = 0; i < 5; i++) {
            Future<String> future = completionService.take();
            System.out.println(future.get());
        }
        System.out.println("End");
    }
}