多线程的意义
- 充分的利用多核机器性能,如果在单核系统上运行,多线程只是利用时间片的概念实现的假多线程。
- 防止阻塞
多线程的创建
1. 继承Thread类
public class myThread_1 extends Thread{
@Override
public void run(){
System.out.println("My thread is running");
}
public void createThread_1(){
Thread thread = new Thread();
thread.start();
}
public void createThread_1_1(){
Thread thread = new Thread(){
@Override
public void run(){
System.out.println("Thread Running");
}
};
thread.start();
}
}
2.实现Runnable方法
public class myThread_2 implements Runnable {
@Override
public void run() {
System.out.println("MyRunnable running");
}
public void createMyThread_2(){
Thread thread = new Thread(new myThread_2());
thread.start();
}
public void createMyThread_2_1(){
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("MyRunnable running");
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
3. 实现Callable方法
public class myThread_3{
public void createCallableThread_3 (){
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return new Random().nextInt(100);
}
};
FutureTask<Integer> future = new FutureTask<Integer>(callable);
new Thread(future).start();
try {
Thread.sleep(5000);
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Runnable()与Callable()方式
- Runnable实现的run()方法没有返回值,单纯执行run中的代码。
2.Callable可以获取线程执行的状态和执行进度。通过与Future的联合使用,可以获得多线程的运行结果,并且进行一些操作。
如何实现线程间的通信
在多线程中,不可避免的需要线程之间进行通信 来完成。这里涉及到的概念有:thread.join(), object.wait(), object.notify(), CountdownLatch, CyclicBarrier, FutureTask, Callable
比如说:
如何让两个线程依次执行?我们可以使用thread.join().
public void demo_join(){
final Thread A = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("A is running");
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("B is waiting A");
try {
A.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("B is running");
}
});
B.start();
A.start();
}
那如何让 两个线程按照指定方式有序交叉运行呢?
public void demo_notify(){
final Object lock = new Object();
Thread A = new Thread(new Runnable() {
@Override
public void run() {
synchronized(lock){
System.out.println("A-1");
try{
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("A-2");
}
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
synchronized(lock){
System.out.println("B-1");
lock.notify();
System.out.println("B-2");
}
}
});
A.start();
B.start();
}
四个线程 A B C D,其中 D 要等到 A B C 全执行完毕后才执行,而且 A B C 是同步运行的。
public void demo_countDownlatch(){
int worker = 3;
final CountDownLatch countDownLatch = new CountDownLatch(worker);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("D is waiting for other three threads");
try {
countDownLatch.await();
System.out.println("D is running");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
for (char threadName='A'; threadName <= 'C'; threadName++) {
final String tN = String.valueOf(threadName);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(tN + "is working");
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(tN + "finished");
countDownLatch.countDown();
}
}).start();
}
}
三个运动员各自准备,等到三个人都准备好后,再一起跑
public void demo_CyclicBarrier(){
int runner = 3;
final CyclicBarrier cyclicBarrier = new CyclicBarrier(runner);
for(char name='A';name<='C';name++){
final String name_temp = String.valueOf(name);
new Thread(new Runnable() {
@Override
public void run() {
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
});
}
}