join主要是让父线程等待子线程结束之后父线程才能继续运行
public static void main(String[] args) throws InterruptedException {
MyThread thread1 = new MyThread("线程1");
thread1.start();
thread1.join();
MyThread thread2 = new MyThread("线程2");
thread2.start();
thread2.join();
MyThread thread3 = new MyThread("线程2");
thread3.start();
thread3.join();
}
static class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
super.run();
System.out.println(Thread.currentThread().getName() + "执行了");
}
}
public static void main(String[] args) throws InterruptedException, Exception {
FutureTask<String> task1 = new FutureTask<>(new MyClass());
Thread t1 = new Thread(task1, "线程1");
t1.start();
System.out.println("线程1返回结果" + task1.get());
FutureTask<String> task2 = new FutureTask<>(new MyClass());
Thread t2 = new Thread(task2, "线程2");
t2.start();
System.out.println("线程2返回结果" + task2.get());
FutureTask<String> task3 = new FutureTask<>(new MyClass());
Thread t3 = new Thread(task3, "线程3");
t3.start();
System.out.println("线程3返回结果" + task3.get());
}
static class MyClass implements Callable<String> {
@Override
public String call() throws Exception {
return Thread.currentThread().getName();
}
}
CountDownLatch使用一个计数器,调用await()方法计数器+1,调用countDown()方法计数器-1,当计数器为0时,使用await()方法的线程可以继续运行。
private static CountDownLatch c1 = new CountDownLatch(1);
private static CountDownLatch c2 = new CountDownLatch(1);
public static void main(String[] args) throws InterruptedException, Exception {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
System.out.println("线程1执行");
c1.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程1");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
c1.await();
System.out.println("线程2执行");
c2.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程2");
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
c2.await();
System.out.println("线程3执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程3");
t3.start();
}
private static final Object lock1 = new Object();
private static final Object lock2 = new Object();
public static void main(String[] args) throws InterruptedException, Exception {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock1) {
try {
lock1.wait();
Thread.sleep(500);
System.out.println("线程1执行");
lock1.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "线程1");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock1) {
try {
lock1.notify();
lock1.wait();
System.out.println("线程2执行");
synchronized (lock2) {
lock2.notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "线程2");
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock2) {
try {
lock2.wait();
System.out.println("线程3执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "线程3");
t3.start();
}
private static Lock lock = new ReentrantLock();
private static Condition condition1 = lock.newCondition();
private static Condition condition2 = lock.newCondition();
public static void main(String[] args) throws InterruptedException, Exception {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
condition1.await();
Thread.sleep(500);
System.out.println("线程1执行了");
condition1.signal();
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程1");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
condition1.signal();
condition1.await();
System.out.println("线程2执行");
condition2.signal();
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程2");
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
condition2.await();
System.out.println("线程3执行");
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程3");
t3.start();
}
private static ExecutorService executorService = Executors.newSingleThreadExecutor();
public static void main(String[] args) throws InterruptedException, Exception {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
System.out.println("线程1执行了");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(200);
System.out.println("线程2执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10);
System.out.println("线程3执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "线程3");
executorService.submit(t1);
executorService.submit(t2);
executorService.submit(t3);
}
private static ExecutorService executorService = Executors.newFixedThreadPool(3);
public static void main(String[] args) throws InterruptedException, ExecutionException {
long start = System.currentTimeMillis();
ArrayList<Future<String>> futureList = new ArrayList<Future<String>>();
ArrayList<String> names = new ArrayList<String>();
for (int i = 0; i < 3; i++) {
futureList.add(executorService.submit(new MyCall(i + "")));
}
for (int i = 0; i < 3; i++) {
Future<String> future = futureList.get(i);
String value = future.get();
System.out.println("线程"+value+"执行了");
}
long spendTime = System.currentTimeMillis() - start;
System.out.println("花费时间---"+spendTime);
}
static class MyCall implements Callable<String> {
private String name;
public MyCall(String name) {
this.name = name;
}
@Override
public String call() throws Exception {
switch (name){
case "0":
Thread.sleep(3000);
break;
case "1":
Thread.sleep(2000);
break;
case "2":
Thread.sleep(1000);
break;
}
return name;
}
}
public static void main(String[] args) throws InterruptedException {
MyThread thread1 = new MyThread("线程1");
MyThread thread2 = new MyThread("线程2");
thread1.start();
thread2.start();
}
static class MyThread extends Thread {
static int num;
static Object object = new Object();
public MyThread(String name) {
super(name);
}
@Override
public void run() {
while (true) {
synchronized (object) {
object.notify();
if (num < 100) {
num++;
System.out.println(Thread.currentThread().getName() + ": " + num);
} else {
break;
}
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws InterruptedException {
MyThread thread1 = new MyThread("线程1", 0);
MyThread thread2 = new MyThread("线程2", 1);
thread1.start();
thread2.start();
}
private static volatile int flag = 0;
static class MyThread extends Thread {
private static int number = 0;
private int flag = 0;
private static ReentrantLock lock = new ReentrantLock();
private static Condition condition = lock.newCondition();
public MyThread(String name, int flag) {
super(name);
this.flag = flag;
}
@Override
public void run() {
while (number < 100) {
lock.lock();
if (number >= 100) break;
if (number % 2 == this.flag) {
number++;
System.out.println(currentThread().getName() + ":" + number);
} else {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
condition.signal();
lock.unlock();
}
}
}
//两个线程,一个打印奇数,一个打印偶数
public class OneToHundred{
static volatile int flag = 0;
public static void main(String[] args){
new Thread(new Task1(),"A").start();
new Thread(new Task2(),"B").start();
}
}
class Task1 implements Runnable{
@Override
public void run(){
int i = -2;
while(i<=99){
if(OneToHundred.flag == 0){
i+=2;
System.out.println("a:" + i);
OneToHundred.flag = 1;
}
}
}
}
class Task2 implements Runnable{
@Override
public void run(){
int i = -1;
while(i<=98){
if(OneToHundred.flag == 1){
i+=2;
System.out.println("b:" + i);
OneToHundred.flag = 0;
}
}
}
}