JAVA创建线程的三种方法及创建线程的简写方法、简单写法
1、通过实现 Runnable 接口;
public class TestThread {
public static void main ( String [ ] args) {
Runnable r1 = ( ) -> {
String threadName = Thread . currentThread ( ) . getName ( ) ;
System . out. println ( "Running thread: " + threadName) ;
for ( int i = 0 ; i < 7 ; i++ ) {
System . out. println ( "Thread name: " + threadName + ", " + i) ;
try {
Thread . sleep ( 50 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} ;
Runnable r2 = ( ) -> {
String threadName = Thread . currentThread ( ) . getName ( ) ;
System . out. println ( "Running thread: " + threadName) ;
for ( int i = 0 ; i < 7 ; i++ ) {
System . out. println ( "Thread name: " + threadName + ", " + i) ;
try {
Thread . sleep ( 50 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} ;
Thread t1 = new Thread ( r1) ;
Thread t2 = new Thread ( r2) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
Running thread: Thread - 0
Thread name: Thread - 0 , 0
Running thread: Thread - 1
Thread name: Thread - 1 , 0
Thread name: Thread - 1 , 1
Thread name: Thread - 0 , 1
Thread name: Thread - 0 , 2
Thread name: Thread - 1 , 2
Thread name: Thread - 0 , 3
Thread name: Thread - 1 , 3
Thread name: Thread - 0 , 4
Thread name: Thread - 1 , 4
Thread name: Thread - 1 , 5
Thread name: Thread - 0 , 5
Thread name: Thread - 0 , 6
Thread name: Thread - 1 , 6
Process finished with exit code 0
public class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
public RunnableDemo ( String threadName) {
this . threadName = threadName;
}
@Override
public void run ( ) {
System . out. println ( "Running thread: " + threadName) ;
for ( int i = 0 ; i < 7 ; i++ ) {
System . out. println ( "Thread name: " + threadName + ", " + i) ;
try {
Thread . sleep ( 50 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
public void start ( ) {
System . out. println ( "Starting thread: " + threadName) ;
if ( t== null ) {
t = new Thread ( this , threadName) ;
t. start ( ) ;
}
}
}
public class TestThread {
public static void main ( String [ ] args) {
RunnableDemo demo1 = new RunnableDemo ( "Thread-1" ) ;
demo1. start ( ) ;
RunnableDemo demo2 = new RunnableDemo ( "Thread-2" ) ;
demo2. start ( ) ;
}
}
Starting thread: Thread - 1
Starting thread: Thread - 2
Running thread: Thread - 1
Running thread: Thread - 2
Thread name: Thread - 1 , 0
Thread name: Thread - 2 , 0
Thread name: Thread - 2 , 1
Thread name: Thread - 1 , 1
Thread name: Thread - 2 , 2
Thread name: Thread - 1 , 2
Thread name: Thread - 1 , 3
Thread name: Thread - 2 , 3
Thread name: Thread - 1 , 4
Thread name: Thread - 2 , 4
Thread name: Thread - 1 , 5
Thread name: Thread - 2 , 5
Thread name: Thread - 1 , 6
Thread name: Thread - 2 , 6
Process finished with exit code 0
2、通过继承 Thread 类本身;
package com. example. utils. thread ;
public class ExtendsThread extends Thread {
private Thread t;
private String threadName;
public ExtendsThread ( String threadName) {
this . threadName = threadName;
}
public void run ( ) {
System . out. println ( "Running thread: " + threadName) ;
for ( int i = 0 ; i < 7 ; i++ ) {
System . out. println ( "Thread name: " + threadName + ", " + i) ;
try {
Thread . sleep ( 50 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
public void start ( ) {
System . out. println ( "Starting Thread: " + threadName) ;
if ( t== null ) {
t = new Thread ( this , threadName) ;
t. start ( ) ;
}
}
}
package com. example. utils. thread ;
public class TestThread {
public static void main ( String [ ] args) {
ExtendsThread demo1 = new ExtendsThread ( "Thread-1" ) ;
demo1. start ( ) ;
ExtendsThread demo2 = new ExtendsThread ( "Thread-2" ) ;
demo2. start ( ) ;
}
}
Starting Thread : Thread - 1
Starting Thread : Thread - 2
Running thread: Thread - 1
Thread name: Thread - 1 , 0
Running thread: Thread - 2
Thread name: Thread - 2 , 0
Thread name: Thread - 2 , 1
Thread name: Thread - 1 , 1
Thread name: Thread - 1 , 2
Thread name: Thread - 2 , 2
Thread name: Thread - 2 , 3
Thread name: Thread - 1 , 3
Thread name: Thread - 2 , 4
Thread name: Thread - 1 , 4
Thread name: Thread - 2 , 5
Thread name: Thread - 1 , 5
Thread name: Thread - 1 , 6
Thread name: Thread - 2 , 6
Process finished with exit code 0
3、通过 Callable 和 Future 创建线程;
import java. util. concurrent. Callable ;
import java. util. concurrent. ExecutionException ;
import java. util. concurrent. FutureTask ;
public class CallableThreadTest implements Callable < Integer > {
public static void main ( String [ ] args) {
CallableThreadTest ctt = new CallableThreadTest ( ) ;
FutureTask < Integer > ft = new FutureTask < > ( ctt) ;
for ( int i = 0 ; i < 7 ; i++ ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + " 的循环变量i的值" + i) ;
if ( i== 5 ) {
new Thread ( ft, "有返回值的线程" ) . start ( ) ;
}
}
try {
System . out. println ( "子线程的返回值:" + ft. get ( ) ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
} catch ( ExecutionException e) {
e. printStackTrace ( ) ;
}
}
@Override
public Integer call ( ) throws Exception {
int i = 0 ;
for ( ; i< 5 ; i++ ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + " " + i) ;
}
return i;
}
}
main 的循环变量i的值0
main 的循环变量i的值1
main 的循环变量i的值2
main 的循环变量i的值3
main 的循环变量i的值4
main 的循环变量i的值5
main 的循环变量i的值6
有返回值的线程 0
有返回值的线程 1
有返回值的线程 2
有返回值的线程 3
有返回值的线程 4
子线程的返回值:5
Process finished with exit code 0