十大免费cad网站入口软件,北京做网站建设价格,网站开发项目计划,安康市住房和城乡建设局网站多线程与高并发 线程的创建方式1.继承Thread类 重写run方法2.实现Runnable接口 重写run方法3. 实现Callable 重写call方法#xff0c;配合FutureTask 线程的使用1.线程的状态1.1. 传统操作系统层面5种状态1.2.Java中给线程准备的6种状态 2.线程的常用方法2.1 获取当前线程2.2 … 多线程与高并发 线程的创建方式1.继承Thread类 重写run方法2.实现Runnable接口 重写run方法3. 实现Callable 重写call方法配合FutureTask 线程的使用1.线程的状态1.1. 传统操作系统层面5种状态1.2.Java中给线程准备的6种状态 2.线程的常用方法2.1 获取当前线程2.2 线程的名字2.3 线程的优先级2.4 线程的让步2.5 线程的休眠2.6 线程的强占2.7 守护线程 线程的创建方式
1.继承Thread类 重写run方法
public class MiTest {public static void main(String[] args) {MyJob t1 new MyJob();t1.start();for (int i 0; i 100; i) {System.out.println(main: i);}}}
class MyJob extends Thread{Overridepublic void run() {for (int i 0; i 100; i) {System.out.println(MyJob: i);}}
}2.实现Runnable接口 重写run方法
public class MiTest {public static void main(String[] args) {MyRunnable myRunnable new MyRunnable();Thread t1 new Thread(myRunnable);t1.start();for (int i 0; i 1000; i) {System.out.println(main: i);}}}class MyRunnable implements Runnable{Overridepublic void run() {for (int i 0; i 1000; i) {System.out.println(MyRunnable: i);}}
}lambda方式
Thread t2 new Thread(() - {for (int i 0; i 100; i) {System.out.println(lambda: i);}
});3. 实现Callable 重写call方法配合FutureTask
public class MiTest {public static void main(String[] args) throws ExecutionException, InterruptedException {//1. 创建MyCallableMyCallable myCallable new MyCallable();//2. 创建FutureTask传入CallableFutureTask futureTask new FutureTask(myCallable);//3. 创建Thread线程Thread t1 new Thread(futureTask);//4. 启动线程t1.start();//5. 做一些操作//6. 要结果Object count futureTask.get();System.out.println(总和为 count);}
}class MyCallable implements Callable{Overridepublic Object call() throws Exception {int count 0;for (int i 0; i 100; i) {count i;}return count;}
}线程的使用
1.线程的状态
1.1. 传统操作系统层面5种状态 1.2.Java中给线程准备的6种状态 NEWThread对象被创建出来了但是还没有执行start方法。
RUNNABLEThread对象调用了start方法就为RUNNABLE状态CPU调度/没有调度
BLOCKED、WAITING、TIME_WAITING都可以理解为是阻塞、等待状态因为处在这三种状态下CPU不会调度当前线程
BLOCKEDsynchronized没有拿到同步锁被阻塞的情况
WAITING调用wait方法就会处于WAITING状态需要被手动唤醒
TIME_WAITING调用sleep方法或者join方法会被自动唤醒无需手动唤醒
TERMINATEDrun方法执行完毕线程生命周期到头了
2.线程的常用方法
2.1 获取当前线程
public static void main(String[] args) throws ExecutionException, InterruptedException {// 获取当前线程的方法Thread main Thread.currentThread();System.out.println(main);// Thread[ getName() , getPriority() , group.getName() ];// Thread[main,5,main]
}2.2 线程的名字
public static void main(String[] args) throws ExecutionException, InterruptedException {Thread t1 new Thread(() - {System.out.println(Thread.currentThread().getName());});t1.setName(模块-功能-计数器);t1.start();
}2.3 线程的优先级
其实就是CPU调度线程的优先级、 java中给线程设置的优先级别有10个级别从1~10任取一个整数。 如果超出这个范围会排除参数异常的错误
public static void main(String[] args) throws ExecutionException, InterruptedException {Thread t1 new Thread(() - {for (int i 0; i 1000; i) {System.out.println(t1: i);}});Thread t2 new Thread(() - {for (int i 0; i 1000; i) {System.out.println(t2: i);}});t1.setPriority(1);t2.setPriority(10);t2.start();t1.start();
}2.4 线程的让步
可以通过Thread的静态方法yield让当前线程从运行状态转变为就绪状态。
public static void main(String[] args) throws ExecutionException, InterruptedException {Thread t1 new Thread(() - {for (int i 0; i 100; i) {if(i 50){Thread.yield();}System.out.println(t1: i);}});Thread t2 new Thread(() - {for (int i 0; i 100; i) {System.out.println(t2: i);}});t2.start();t1.start();
}2.5 线程的休眠
Thread的静态方法让线程从运行状态转变为等待状态
sleep有两个方法重载
第一个就是native修饰的让线程转为等待状态的效果第二个是可以传入毫秒和一个纳秒的方法如果纳秒值大于等于0.5毫秒就给休眠的毫秒值1。如果传入的毫秒值是0纳秒值不为0就休眠1毫秒
sleep会抛出一个InterruptedException
public static void main(String[] args) throws InterruptedException {System.out.println(System.currentTimeMillis());Thread.sleep(1000);System.out.println(System.currentTimeMillis());
}2.6 线程的强占
Thread的非静态方法join方法
需要在某一个线程下去调用这个方法
如果在main线程中调用了t1.join()那么main线程会进入到等待状态需要等待t1线程全部执行完毕在恢复到就绪状态等待CPU调度。
如果在main线程中调用了t1.join(2000)那么main线程会进入到等待状态需要等待t1执行2s后在恢复到就绪状态等待CPU调度。如果在等待期间t1已经结束了那么main线程自动变为就绪状态等待CPU调度。
public static void main(String[] args) throws InterruptedException {Thread t1 new Thread(() - {for (int i 0; i 10; i) {System.out.println(t1: i);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t1.start();for (int i 0; i 10; i) {System.out.println(main: i);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (i 1){try {t1.join(2000);} catch (InterruptedException e) {e.printStackTrace();}}}
}2.7 守护线程
默认情况下线程都是非守护线程
JVM会在程序中没有非守护线程时结束掉当前JVM
主线程默认是非守护线程如果主线程执行结束需要查看当前JVM内是否还有非守护线程如果没有JVM直接停止
public static void main(String[] args) throws InterruptedException {Thread t1 new Thread(() - {for (int i 0; i 10; i) {System.out.println(t1: i);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t1.setDaemon(true);t1.start();
}