马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
目次
Thread的常见构造方法
Thread的常见属性
代码示例1
代码示例2
示例代码3
代码示例4
代码示例5
小结
线程制止
代码示例1
代码示例2
代码示例3
代码示例4
小结
线程等候
获取当火线程的引用
Thread的常见构造方法
举例
- Thread t1 = new Thread();
- Thread t2 = new Thread(new MyRunnable());
- Thread t3 = new Thread("这是我的名字");
- Thread t4 = new Thread(new MyRunnable(), "这是我的名字");
复制代码 代码示例
- public class Demo06 {
- public static void main(String[] args) {
- Thread t=new Thread(()->{
- while(true){
- System.out.println("hello thread");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- },"这是新线程");
- t.start();
- }
- }
复制代码 通过jconsole.exe观察
Thread的常见属性
| 属性 | 获取方法 | | ID | getId() | | 名称 | getName() | | 状态 | getState() | | 优先级 | getPriority() | | 是否配景进程 | isDaemon() | | 是否存活 | isAlive() | | 是否被制止 | isInterrupted() |
ID 是线程的唯一标识,差别线程不会重复;
名称是各种调试工具用到;
状态体现线程当前所处的一个环境;
优先级高的线程理论上来说更轻易被调治到;
关于配景线程,记取一点:JVM会在一个进程的全部非配景线程竣过后,才会竣事运行;
是否存活,即简单的明白,为 run 方法是否运行竣事了
代码示例1
- public class Demo06 {
- public static void main(String[] args) {
- Thread t=new Thread(()->{
- while(true){
- System.out.println("hello thread");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- },"这是新线程");
- System.out.println(t.getId());
- System.out.println(t.getName());
- System.out.println(t.getState());
- System.out.println(t.getPriority());
- System.out.println(t.isDaemon());
- System.out.println(t.isAlive());
- System.out.println(t.isInterrupted());
- t.start();
- }
- }
复制代码 运行结果
代码示例2
- public class Demo07 {
- public static void main(String[] args) throws InterruptedException {
- Thread t=new Thread(()->{
- System.out.println("线程开始");
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- System.out.println("线程结束");
- });
- System.out.println(t.isAlive());
- t.start();
- System.out.println(t.isAlive());
- Thread.sleep(3000);
- System.out.println(t.isAlive());
- }
- }
复制代码 运行结果
示例代码3
- public class Demo08 {
- private static boolean isQuit=false;
- public static void main(String[] args) throws InterruptedException {
- Thread t=new Thread(()->{
- while(!isQuit){
- System.out.println("线程工作中");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- System.out.println("线程工作完毕!");
- });
- t.start();
- Thread.sleep(5000);
- isQuit=true;
- System.out.println("设置 isQuit 为 true");
- }
- }
复制代码 运行结果
代码示例4
- public class Demo09 {
- public static void main(String[] args) throws InterruptedException {
- Thread t=new Thread(()->{
- //Thread类内部有一个标志位,可以用来判定当前的循环是否要结束
- while(!Thread.currentThread().isInterrupted()){
- System.out.println("线程工作中");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- //throw new RuntimeException(e);
- //1.假装没听见,循环继续正常执行
- e.printStackTrace();
- //2.加上一个bread,表示让线程立即结束
- //break;
- //3.做一些其它工作,完成之后再结束
- break;
- }
- }
- });
- t.start();
- Thread.sleep(5000);
- System.out.println("让 t 线程终止");
- t.interrupt();
- }
- }
复制代码 运行结果
代码示例5
- public class Demo10 {
- public static void main(String[] args) throws InterruptedException {
- Thread t=new Thread(()->{
- for (int i = 0; i < 5; i++) {
- System.out.println("t 线程工作中");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- });
- t.start();
-
- System.out.println("join 等待开始");
- t.join();
- System.out.println("join 等待结束");
- }
- }
复制代码 小结
上面的代码是让主线程来等候 t 线程实行竣事,一旦调用 join,主线程就会触发壅闭,此时 t 线程就可以乘隙完成后续的工作,不绝到壅闭到 t 实行完毕,join才会排除壅闭。
即:哪个线程调用 join(),哪个线程就会被壅闭。
代码示例6
- public class Demo11 {
- public static void main(String[] args) throws InterruptedException {
- Thread t=new Thread(()->{
- while(true){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- });
- System.out.println(t.getState());
- t.start();
- for (int i = 0; i < 5; i++) {
- System.out.println(t.getState());
- Thread.sleep(1000);
- }
- System.out.println(t.getState());
- }
- }
复制代码 运行结果
线程制止
常见的两种方式:
1.通过共享的标记来举行沟通;
2.调用 interrupt() 方法来关照。
代码示例1
- public class Demo11 {
- private static class MyRunnable implements Runnable{
- public volatile boolean isQuit=false;
- @Override
- public void run() {
- while(!isQuit) {
- System.out.println("开始");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- System.out.println("结束");
- }
- }
- public static void main(String[] args) throws InterruptedException {
- MyRunnable target=new MyRunnable();
- Thread t=new Thread(target);
- t.start();
- Thread.sleep(5000);
- target.isQuit=true;
- }
- }
复制代码 运行结果
代码示例2
- public class Demo11 {
- private static class MyRunnable implements Runnable{
- @Override
- public void run() {
- while(!Thread.interrupted()) {
- System.out.println("开始");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- System.out.println("结束");
- }
- }
- public static void main(String[] args) throws InterruptedException {
- MyRunnable target=new MyRunnable();
- Thread t=new Thread(target);
- t.start();
- Thread.sleep(5000);
- t.interrupt();
- }
- }
复制代码 运行结果
代码示例3
- public class Demo11 {
- private static class MyRunnable implements Runnable{
- @Override
- public void run() {
- while(true){
- System.out.println(Thread.currentThread().isInterrupted());
- }
- }
- }
- public static void main(String[] args) throws InterruptedException {
- MyRunnable target=new MyRunnable();
- Thread t=new Thread(target);
- t.start();
- Thread.sleep(2000);
- t.interrupt();
- }
- }
复制代码 运行结果
代码示例4
- public class Demo11 {
- private static class MyRunnable implements Runnable{
- @Override
- public void run() {
- while(true){
- System.out.println(Thread.interrupted());
- }
- }
- }
- public static void main(String[] args) throws InterruptedException {
- MyRunnable target=new MyRunnable();
- Thread t=new Thread(target);
- t.start();
- Thread.sleep(2000);
- t.interrupt();
- }
- }
复制代码 运行结果
小结
假如线程由于调用wait/join/sleep等方法而壅闭挂起,则以InterruptedException非常的情势关照,扫除制止标记,当出现InterruptedException的时间,要不要竣事线程取决于catch中代码的写法,可以选择忽略这个非常,也可以选择跳出循环竣事线程;
1.Thread.interrupted()判断当火线程的制止标记被设置,扫除制止标记;
2.Thread.currentThread().isInterrupted()判断指定线程的制止标记被设置,不清楚制止标记;
这两种方法关照收到的更及时,及时线程正在sleep也可以立刻收到。
线程等候
获取当火线程的引用
| 方法 | 属性 | | public static Thread currentThread() | 返回当火线程对象的引用 |
- public class ThreadDemo {
- public static void main(String[] args) {
- Thread thread = Thread.currentThread();
- System.out.println(thread.getName());
- }
- }
复制代码
|