多线程的实现以及线程之间的状态转换代码体现
多线程的几种实现方法
public class 多线程实例 {
@Test
public void test1() {
class A extends Thread {
@Override
public void run() {
System.out.println("A run");
}
}
A a = new A();
a.start();
}
@Test
public void test2() {
class B implements Runnable {
@Override
public void run() {
System.out.println("B run");
}
}
B b = new B();
new Thread(b).start();
}
@Test
public void test3() {
Callable callable = new Callable() {
int sum = 0;
@Override
public Object call() throws Exception {
for (int i = 0;i < 5;i ++) {
sum += i;
}
return sum;
}
};
FutureTask futureTask = new FutureTask(callable);
new Thread(futureTask).start();
try {
System.out.println(futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
@Test
public void test4() {
ExecutorService executorService = Executors.newFixedThreadPool(5);
executorService.execute(new Thread());
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println("runnable");
}
});
Future future = executorService.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "a";
}
});
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
ArrayList<String> list = new ArrayList<>();
for (int i = 0;i < 5;i ++ ) {
int finalI = i;
Future future1 = executorService.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "res" + finalI;
}
});
try {
list.add((String) future1.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
for (String s : list) {
System.out.println(s);
}
}
}
---------------------
作者:程序员江湖
来源:CSDN
原文:https://blog.csdn.net/a724888/article/details/80197160
版权声明:本文为博主原创文章,转载请附上博文链接!
线程状态转换
public class 线程的状态转换 {
class t implements Runnable {
private String name;
public t(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(name + "run");
}
}
@Test
public void test1() throws InterruptedException {
Thread dad = new Thread(new Runnable() {
Thread son = new Thread(new t("son"));
@Override
public void run() {
System.out.println("dad init");
son.start();
try {
son.join();
System.out.println("dad run");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
dad.start();
}
@Test
public void test2(){
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t1 run");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.start();
}
public static void main(String[] args) {
test4();
Thread.yield();
}
public static void test4() {
class A {
synchronized void run() {
while (true) {
System.out.println("run");
}
}
}
A a = new A();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t1 get lock");
a.run();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t2 get lock");
a.run();
}
}).start();
}
volatile static int flag = 1;
volatile static Object o = new Object();
public void test5() {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (o) {
while (true) {
if (flag == 0) {
try {
Thread.sleep(2000);
System.out.println("thread1 wait");
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("thread1 run");
System.out.println("notify t2");
flag = 0;
o.notify();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (o) {
if (flag == 1) {
try {
Thread.sleep(2000);
System.out.println("thread2 wait");
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("thread2 run");
System.out.println("notify t1");
flag = 1;
o.notify();
}
}
}
}).start();
}
}
---------------------
作者:程序员江湖
来源:CSDN
原文:https://blog.csdn.net/a724888/article/details/80197160
版权声明:本文为博主原创文章,转载请附上博文链接!