table of Contents
Inherit the Thread class
step
- Define a subclass of the Thread class, rewrite the run method, and implement the relevant logic. The run() method is the business logic method to be executed by the thread
- Create a custom thread subclass object
- Call the star() method of the subclass instance to start the thread
Code
public class MyThread {
public static void main(String[] args) {
MyThreadInner myThreadInner = new MyThreadInner();
myThreadInner.start();
}
static class MyThreadInner extends Thread{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " run()方法正在执行...");
}
}
}
Implement the Runable interface
step
- Define the Runnable interface implementation class MyRunnable, and override the run() method
- Create the MyRunnable instance myRunnable, and create the Thead object with myRunnable as the target. The Thread object is the real thread object
- Call the start() method of the thread object
Code
public class MyRunnable {
public static void main(String[] args) {
MyRunnableInner myRunnableInner = new MyRunnableInner();
Thread thread = new Thread(myRunnableInner);
thread.start();
}
private static class MyRunnableInner implements Runnable{
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " run()方法执行中...");
}
}
Use Callable and Future to create threads
step
- Create a class myCallable that implements the Callable interface
- Create a FutureTask object with myCallable as a parameter
- Create a Thread object with FutureTask as a parameter
- Call the start() method of the thread object
Code
public class MyCallable {
public static void main(String[] args) {
FutureTask<Integer> future = new FutureTask<Integer>(new MyCallableInner());
Thread thread = new Thread(future);
thread.start();
try {
Thread.sleep(1000);
System.out.println("返回结果 " + future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " main()方法执行完成");
}
private static class MyCallableInner implements Callable<Integer> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
@Override
public Integer call() throws Exception {
System.out.println(Thread.currentThread().getName() + " call()方法执行中...");
return 1;
}
}
}
Use the Executor framework to create a thread pool
Executors provides a series of factory methods to create a thread pool, and the returned thread pool implements the ExecutorService interface.
There are mainly newFixedThreadPool, newCachedThreadPool, newSingleThreadExecutor, newScheduledThreadPool. These four thread pools will be introduced in detail later:
Code
public class SingleThreadExecutor {
public static void main(String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
for(int i=0;i<10;i++){
MyRunnableInner myRunnableInner = new MyRunnableInner();
executorService.execute(myRunnableInner);
}
System.out.println("线程任务开始执行");
executorService.shutdown();
}
private static class MyRunnableInner implements Runnable{
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " run()方法执行中...");
}
}
}