最近和非常好的朋友聊天,我们聊到一些优化方面的技术,他说他写js曾经遇到过页面加载很慢,因为页面中有20多个图片,全是高清的,我问他那你是怎么解决的,他说,这些图片最后他都使用了异步去获取,最终解决了问题,然后我突然想到,这个和多线程的思想是一样的,如果一个方法中要执行ABC三件事,那如果ABC不互相关联,那么怎么优化方法呢,怎样压缩时间呢,答案就是,能异步的就异步。下面就来说说java的异步Thread类
我们还是new一个他
/**
* 线程的启动 最终调用jni的start0()方法 让jvm去处理
* 然后jvm回调 run方法最终创建了一个线程
* public synchronized void start() 这一步是有锁的
*
* @param args
*/
public static void main(String[] args) {
new Thread(()->{
System.out.println("3443");
},"杨乐乐").start();
}
private native void start0();
挺简单的,但是如果不用,其实还是挺陌生的呢。
然后讲讲他的生命周期
源码中他有6中状态
public enum State {
NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED;
}
New状态
Thread thread=new Thread(()->{
});
System.out.println(thread.getState());
RUNNABLE状态
Thread thread = new Thread(() -> {
});
// 启动 这个时候线程不一定执行 只是通知cpu 只有cpu分配了资源才会执行
thread.start();
System.out.println(thread.getState());
BLOCK状态
Object obj = new Object();
new Thread(() -> {
synchronized (obj) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Thread thread = new Thread(() -> {
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
while (true) {
Thread.sleep(1000);
System.out.println(thread.getState());
}
WAITING状态 通过wait()方法
Object obj = new Object();
Thread thread = new Thread(() -> {
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
while (true) {
Thread.sleep(1000);
System.out.println(thread.getState());
}
TIMED_WAITING状态 通过Thread.sleep(100000)实现
Object obj = new Object();
Thread thread = new Thread(() -> {
synchronized (obj) {
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
while (true) {
Thread.sleep(1000);
System.out.println(thread.getState());
}
//TERMINATED状态 终止执行
Thread thread = new Thread(() -> {
});
thread.start();
System.out.println(thread.getState());
以上就是他的生命周期,然后说说他经常用的集中方法wait notify join yield
1 yield
private static volatile Map<String, AtomicInteger> count = new ConcurrentHashMap<>();
/**
* yeild方法会在几个线程一起执行时 让步cpu资源,让其他线程先执行
* 但是也不是一定让出,他和start一样只是通知系统要让出,具体让不让出不一定
*/
static class Y implements Runnable {
private String name;
private boolean isYield;
public Y(String name, boolean isYield) {
this.name = name;
this.isYield = isYield;
}
@Override
public void run() {
long l = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
if (isYield) Thread.yield();
AtomicInteger atomicInteger = count.get(name);
if (null == atomicInteger) {
count.put(name, new AtomicInteger(1));
continue;
}
atomicInteger.addAndGet(1);
count.put(name, atomicInteger);
}
System.out.println("线程编号:" + name + " 执行完成耗时:" + (System.currentTimeMillis() - l) + " (毫秒)" + (isYield ? "让出CPU----------------------" : "不让CPU"));
}
}
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
if (i < 10) {
new Thread(new Y(String.valueOf(i), true)).start();
continue;
}
new Thread(new Y(String.valueOf(i), false)).start();
}
}
2 join
/**
* 调用join方法作用是要等子线程执行完 再执行主线程
* 就是主线程被wait了 然后 子线程执行完 再执行一个notify
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
System.out.println("thread before");
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread after");
});
thread.start();
System.out.println("main begin!");
thread.join();
System.out.println("main end!");
}
后面两个我想算是比较频发的,就不说了。