directory

introduce

Implementation examples

Direct call

Implement the Runnable interface

Inheriting Thread implementation

Get the return value

Implement join to get the return value

Implement Callable and FutureTask to get the returned data

Thread pools are returned in conjunction with Callable



introduce

This article introduces Java thread implementations, implementation methods with and without return values, and methods for getting return values

Future futureTask or Thread pool executorService. submit(Callable<t> implementation class);

Java thread lock ReentrantLock Non-fair lock ReentrantReadWriteLock ReentrantLock is a synchronized, conditional, read and write lock

Yushen.blog.csdn.net/article/det…

 

Implementation examples

Direct call

public class test1 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { final int nowi = i; new Thread(new Runnable() { @Override public void run() { for (int j = 0; j < 3; J ++) {system.out.println (nowi+" task running "+j); } } }).start(); }}}Copy the code

 

Implement the Runnable interface

public class test1 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { new Thread(new t1(i+"")).start(); } } } class t1 implements Runnable{ private String taskName = ""; public t1(String taskName){ this.taskName=taskName; } @Override public void run() { for (int i = 0; i <3; I ++) {system.out.println (taskName+" task executing: "+ I); }}}Copy the code

 

Inheriting Thread implementation

public class test1 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { // new Thread(new t1(i+"")).start(); new t2(i+"").start(); } } } class t2 extends Thread{ private String taskName = ""; public t2(String taskName){ this.taskName=taskName; } @Override public void run() { for (int i = 0; i <3; I ++) {system.out.println (taskName+" task executing: "+ I); }}}Copy the code

 

 

Get the return value

 

Implement join to get the return value

public class test1 { public static void main(String[] args) { t2 t = new t2(9); t.start(); try { t.join(); System.out.println(" num="+ t.getnum ()); } catch (InterruptedException e) { e.printStackTrace(); } } } class t2 extends Thread{ private int num = 0; public t2(int num){ this.num=num; } public int getNum(){ return this.num; } @Override public void run() { for (int i = 0; i <3; I ++) {system.out.println (" task executing ++ : the "+ I +" time "); this.num++; }}}Copy the code

 

Implement Callable<t> and FutureTask to get the returned data

public class test1 { public static void main(String[] args) { FutureTask<String> task = new FutureTask<String>(new t3(9)); new Thread(task).start(); if (! Task.isdone ()) {system.out.println (" Task not completed, please wait!" ); } try {system.out.println (" task return: "+ task.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } class t3 implements Callable<String> { private int num = 0; public t3(int num) { this.num = num; } @Override public String call() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return num + "5578"; }}Copy the code

methods

  • IsDone: Uses the state variable to determine whether the call method was executed
  • Get: Returns the return value of the call method if it has finished executing, and blocks if it has not

 

Thread pools are returned in conjunction with Callable<T>

Public class test1 {public static void main(String[] args) {// Create a thread pool newCachedThreadPool = Executors.newCachedThreadPool(); / / thread by thread pool management MyCallable Future < String > Future = newCachedThreadPool. Submit (new t3 (98)); // If (! Future.isdone ()) {system.out.println (" Running! ); } try { System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } the finally {/ / close the thread pool newCachedThreadPool. Shutdown (); } } } class t3 implements Callable<String> { private int num = 0; public t3(int num) { this.num = num; } @Override public String call() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return num + "5578"; }}Copy the code

 

 

 

Java thread lock ReentrantLock Non-fair lock ReentrantReadWriteLock ReentrantLock is a synchronized, conditional, read and write lock

Yushen.blog.csdn.net/article/det…

 

 

 

ok

 

 

 

 

 

Continuously updated