Java geek
Related reading:
Java Concurrent programming (1) Knowledge map Java concurrent programming (2) Atomic Java concurrent programming (3) Visibility Java concurrent programming (4) Sequential Java concurrent programming introduction (6) Synchronized usage Java Concurrency programming Introduction (7) Easy to understand wait and Notify and use scenarios Java concurrency programming Introduction (8) Thread lifecycle Java concurrency programming Introduction (9) Deadlock and deadlock bit Java concurrency programming Introduction (10) lock optimization Introduction to Concurrent Programming in Java (11) Flow limiting scenarios and Spring Flow Limiting scenarios Introduction to Concurrent programming in Java (12) Producer and Consumer Patterns – Introduction to Concurrent programming in Java with code templates (13) Read/write lock and cache templates (14) CountDownLatch application scenarios Introduction to Concurrent Programming (CyclicBarrier) Introduction to Concurrent programming (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier Java Concurrent programming Introduction (19) Asynchronous task scheduling tool CompleteFeature Java concurrent programming Introduction (20) Common locking scenarios and locking tools
Here are a few ways to create a thread without going into detail.
Implement the Runnable interface
public class RunnableDemo {
public static void main(String[] args) {
The Thread constructor injects the implementation class and starts it
new Thread(newRunnableWorker()).start(); }}// Implement the Runnable interface
class RunnableWorker implements Runnable {
public void run(a) {
for (int i = 0; i < 10; i++) { System.out.println(i); }}}Copy the code
Inherit Thread class
public class ThreadDemo extends Thread {
public static void main(String[] args) {
new ThreadDemo().start();
}
@Override
public void run(a) {
for (int i = 0; i < 10; i++) { System.out.println(i); }}}Copy the code
Three, implement Callable interface
public class FutureDemo {
public static void main(String[] args) {
FutureTask<Integer> task = new FutureTask(new CallableWorker());
new Thread(task).start();
try {
System.out.println(task.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch(ExecutionException e) { e.printStackTrace(); }}}class CallableWorker implements Callable<Integer> {
public Integer call(a) throws Exception {
return 10; }}Copy the code
4. Executors Build
public class ExecutorsDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// Create a thread pool
ExecutorService executorService = Executors.newFixedThreadPool(1);
// Submit anonymous Callable
Future<Integer> future = executorService.submit(new Callable<Integer>() {
public Integer call(a) throws Exception {
return 10; }});// Get the thread execution return valueSystem.out.println(future.get()); }}Copy the code
end.
<– Read the mark, left like!