目录
线程的4种创建方式:
第一种:
public class A1 {
public static void main(String[] args) {
Thread t1=new My1();
t1.start();
// t1.run();
System.out.println(Thread.currentThread()+" end...");
}
}
class My1 extends Thread {
@Override
public void run() {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+" end...");
}
}.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+" end...");
}
}
第二种:
public class A2 {
public static void main(String[] args) {
Thread t1 = new Thread(new My2());
t1.start();
// 省略了final
User tmp=new User();
Thread t11=new Thread(new Runnable() {
public void run() throws RuntimeException {
tmp.id=100L;
}
});
t11.start();
try {
t11.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(tmp);
//语法错误
new Thread(new Runnable() {
public void run() throws IOException {
}
}).start();
new Thread(() -> {
System.out.println(Thread.currentThread());
if(2>3)
throw new RuntimeException("asdfasdf");
}).start();
}
}
class My2 implements Runnable {
public void run() {
System.out.println(Thread.currentThread());
}
}
class User{
Long id;
String name;
}
第三种:
public class A3 {
public static void main(String[] args) {
// MyCallable2<Number> m2=new MyCallable2<Integer>() 错误
MyCallable2<Number> m2 = new MyCallable2<>();
m2.setT(123);
}
}
class MyCallable1 implements Callable {
@Override
public Object call() throws Exception {
if (1 == 2)
return "asdfsd";
else
return 123;
}
}
class MyCallable2<T> implements Callable<T> {
private T t;
@Override
public T call() throws Exception {
return t;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
class MyCallable3<T extends Number> implements Callable<T>{
@Override
public T call() throws Exception {
// TODO Auto-generated method stub
return null;
}
}
public class A31 {
public static void main(String[] args) throws Exception {
// FutureTask<Number> ft = new FutureTask<>(new My3());
// new Thread(ft).start();
Future<Number> ft = new FutureTask<>(new My3());
if (ft != null && ft instanceof Runnable) {
Runnable r = (Runnable) ft;
new Thread(r).start();
}
Number obj = ft.get();
System.out.println(obj.intValue());
FutureTask<Number> ft2 = new FutureTask<>(() -> {
return 123;
});
new Thread(ft2).start();
int res = ft2.get().intValue();
Future<Number> ft3 = new FutureTask<>(new Callable<Number>() {
@Override
public Number call() throws Exception {
return 999;
}
});
if (ft3 != null && ft3 instanceof Runnable) {
Runnable r2 = (Runnable) ft3;
new Thread(r2).start();
}
}
}
class My3 implements Callable<Number> {
@Override
public Number call() throws Exception {
System.out.println(Thread.currentThread());
return 123;
}
}
第四种:
public class A4 {
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++)
es.execute(() -> {
System.out.println(Thread.currentThread());
});
//第二种写法
// es.execute(new Runnable() {
// public void run() {
//
// }
// });
//
// es.submit(()->{
// return "abc";
// });
// es.submit(()->{
// System.out.println(Thread.currentThread());
// });
}
}