wordpress做得比较大的网站,建筑方案设计流程步骤,网站设计 图片,wordpress自定义头像插件2.2.6 线程的强占
Thread的非静态方法join方法 需要在某一个线程下去调用这个方法 如果在main线程中调用了t1.join()#xff0c;那么main线程会进入到等待状态#xff0c;需要等待t1线程全部执行完毕#xff0c;在恢复到就绪状态等待 CPU调度。 如果在main线程中调用了t1.j…2.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.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();
}
2.2.8 线程的等待和唤醒
可以让获取synchronized锁资源的线程通过wait方法进去到锁的**等待池**并且会释放锁资源 可以让获取synchronized锁资源的线程通过notify或者notifyAll方法将等待池中的线程唤醒添加到**锁池**中 notify随机的唤醒等待池中的一个线程到锁池 notifyAll将等待池中的全部线程都唤醒并且添加到锁池 在调用wait方法和notify以及norifyAll方法时必须在synchronized修饰的代码块或者方法内部才可以因为要操作基于某个对象 的锁的信息维护。
public static void main(String[] args) throws InterruptedException {
Thread t1 new Thread(() - {
sync();
},t1);
Thread t2 new Thread(() - {
sync();
},t2);
t1.start();
t2.start();
Thread.sleep(12000);
synchronized (MiTest.class) {
MiTest.class.notifyAll();
}
}
public static synchronized void sync() {
try {for (int i 0; i 10; i) {
if(i 5) {
MiTest.class.wait();
}
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
2.3 线程的结束方式
线程结束方式很多最常用就是让线程的run方法结束无论是return结束还是抛出异常结束都可以
2.3.1 stop方法不用
强制让线程结束无论你在干嘛不推荐使用当然当然方式但是他确实可以把线程干掉
public static void main(String[] args) throws InterruptedException {
Thread t1 new Thread(() - {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();}
});
t1.start();
Thread.sleep(500);
t1.stop();
System.out.println(t1.getState());
}
2.3.2 使用共享变量很少会用
这种方式用的也不多有的线程可能会通过死循环来保证一直运行。 咱们可以通过修改共享变量在破坏死循环让线程退出循环结束run方法
static volatile boolean flag true;
public static void main(String[] args) throws InterruptedException {
Thread t1 new Thread(() - {
while(flag){
// 处理任务
}
System.out.println(任务结束);
});
t1.start();
Thread.sleep(500);
flag false;
}
2.3.3 interrupt方式
共享变量方式
public static void main(String[] args) throws InterruptedException {
// 线程默认情况下 interrupt标记位false
System.out.println(Thread.currentThread().isInterrupted());
// 执行interrupt之后再次查看打断信息
Thread.currentThread().interrupt();
// interrupt标记位ture
System.out.println(Thread.currentThread().isInterrupted());
// 返回当前线程并归位为false interrupt标记位ture
System.out.println(Thread.interrupted());
// 已经归位了
System.out.println(Thread.interrupted());
//
Thread t1 new Thread(() - {
while(!Thread.currentThread().isInterrupted()){
// 处理业务
}
System.out.println(t1结束);
});
t1.start();
Thread.sleep(500);
t1.interrupt();
}
通过打断WAITING或者TIMED_WAITING状态的线程从而抛出异常自行处理 这种停止线程方式是最常用的一种在框架和JUC中也是最常见的
public static void main(String[] args) throws InterruptedException {
Thread t1 new Thread(() - {
while(true){
// 获取任务
// 拿到任务执行任务
// 没有任务了让线程休眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(基于打断形式结束当前线程);
return;
}
}
});
t1.start();
Thread.sleep(500);
t1.interrupt();
}
wait和sleep的区别 ● 单词不一样。 ● sleep属于Thread类中的static方法、wait属于Object类的方法 ● sleep属于TIMED_WAITING自动被唤醒、wait属于WAITING需要手动唤醒。 ● sleep方法在持有锁时执行不会释放锁资源、wait在执行后会释放锁资源。 ● sleep可以在持有锁或者不持有锁时执行。 wait方法必须在只有锁时才可以执行。 wait方法会将持有锁的线程从owner扔到WaitSet集合中这个操作是在修改ObjectMonitor对象如果没有持有synchronized锁的 话是无法操作ObjectMonitor对象的。