1.Thread
Thread thread=new Thread(){ @Override public void run() { System.out.println(this.getName()+ 1); } }; thread.start();
2.Runnable
Runnable runnable = new Runnable() { @Override public void run() { System.out.println(2); } }; new Thread(runnable).start();
3.线程池 ---阿里不建议使用,容易导致oom
ExecutorService executorService= Executors.newFixedThreadPool(10); Runnable runnable = new Runnable() { @Override public void run() { } }; executorService.submit(runnable);
4.线程池
ExecutorService executor = new ThreadPoolExecutor(10, 10, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); Callable callable=new Callable() { @Override public Object call() throws Exception { return null; } }; Future<Object> future= executor.submit(callable);