1 Star 14 Fork 2

Albert.Yang/concurrent-practice

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ThreadJoin_06.java 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
Albert.Yang 提交于 2021-01-28 00:29 +08:00 . # 提交lock的内容编写
package com.albert.concurrent.book.chaptertwo;
import com.albert.concurrent.TestApplication;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* 等待线程结束join()方法和礼让线程yeild()方法练习
*
* <p>
*
* @author Albert
* @date 2020/8/26 19:16
*/
@SpringBootTest(classes = TestApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class ThreadJoin_06 {
private static int i=0;
public static class ChangeIntValue extends Thread{
@Override
public void run() {
for(i=0;i<1000;i++);
//让出线程资源(让出CPU资源,但是会重新竞争CPU资源)
Thread.yield();
}
}
/**
* 测试join()方法
*/
@SneakyThrows
@Test
public void testJoin() {
ChangeIntValue changeIntValue = new ChangeIntValue();
changeIntValue.start();
//等待线程结束,等待指定线程执行结束
// changeIntValue.join();
changeIntValue.join(100);
System.out.println(i);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/zztiyjw/concurrent-practice.git
git@gitee.com:zztiyjw/concurrent-practice.git
zztiyjw
concurrent-practice
concurrent-practice
master

搜索帮助