java 程序、进程 、线程,cpu,并行、并发、启动线程两种方式

 

1、重写 Thread 父类方法 后创建实例调用 start 方法

2、将创建自实现 Runable 接口后的实例 作为参数传递给 Thread 的构造方法

两个条件同时存在,那个生效?

 new Thread(/* condition 1 */threadTest2) {
            @Override
            /* condition 2 */
            public void run() {
                Thread.currentThread().setName("线程3");
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName() + ' ' + "threadTest3" + i);
                }
            }
        }.start();

条件一: 将创建自实现 Runable 接口后的实例 作为参数传递给 Thread 的构造方法

条件二: 重写 Thread 父类方法 后创建实例调用 start 方法 (创建匿名子类的匿名对象)

  

相同:

最后调用的都是 Thread 的start 的方法 

创建的线程对象,都是Thread类或其子类的实例。

区别:

Thread 方式 线程间不可以共享数据,除非变量加 static 变为 静态变量 (每次创建实例,会有一个新的副本)。

Runable 优点:

线程间可以共享数据

class threadTest2 implements Runnable {
    private int num = 100;

    @Override
    public void run() {
        for (int i = num; i > 0; i--) {
            System.out.println(Thread.currentThread().getName() + ' ' + "threadTest2 卖票" + i);
        }
        System.out.println(Thread.currentThread().getName() + ' ' + "票卖完了");
    }
}



  threadTest2 threadTest2 = new threadTest2();

        threadTest2 threadTest2_1 = new threadTest2();

        threadTest2 threadTest2_2 = new threadTest2();

        new Thread(/* condition 1 */threadTest2) {
        }.start();


        new Thread(threadTest2_1).start();
        new Thread(threadTest2_2).start();


package src.ThreadH;

class RunableDemo1 implements Runnable {
    private int length = 6;

    public void run() {
        for (int i = length; i > 0; i--) {
            if (length > 0) {
                System.out.println(Thread.currentThread().getName() + " 卖货 " + length--);
            } else {
                System.out.println(Thread.currentThread().getName() + " 卖完了 " + i);
            }
        }
    }
}

class test_ThreadAndRunableDifference {
    public static void main(String[] args) {
        /* Runable 操作开始*/
        RunableDemo1 demon3 = new RunableDemo1();
        Thread demo3 = new Thread(demon3);
        Thread demo4 = new Thread(demon3);
        Thread demo5 = new Thread(demon3);
        demo3.start();
        demo4.start();
        demo5.start();
        /*Runable 操作结束*/
    }
}

join

要让主线程等待 demo3 线程完成执行,您需要先启动线程(demo3.start();),然后再调用 join() 方法。这样,join() 方法会让主线程等待,直到 demo3 线程运行完成。

demo3 线程会先开始执行,然后主线程会在 demo3.join(); 处阻塞,直到 demo3 线程执行完毕。之后,主线程会继续执行其余的循环。

 

package src.ThreadH;

class RunableDemo1 implements Runnable {
    private int length = 6;

    @Override
    public void run() {
        for (int i = length; i > 0; i--) {
            System.out.println(Thread.currentThread().getName() + " 卖完了 " + i);
        }
    }
}

class test_ThreadAndRunableDifference {
    public static void main(String[] args) throws InterruptedException {
        RunableDemo1 demon3 = new RunableDemo1();
        Thread demo3 = new Thread(demon3);
        /* Runable 操作开始*/
        demo3.start();
        /*Runable 操作结束*/
        
        // 等待 demo3 线程完成
        demo3.join();
        
        for (int i = 0; i < 10; i++) {
            if (i == 3) {
                // 可以在这里添加一些特定操作
            } else {
                System.out.println(Thread.currentThread().getName() + " 主线程 " + i);
            }
        }
    }
}

isAlive

package src.ThreadH;

class RunableDemo1 implements Runnable {
    private int length = 6;

    public void run() {
        for (int i = length; i > 0; i--) {
            System.out.println(Thread.currentThread().getName() + " 卖完了 " + i);
        }
        System.out.println(Thread.currentThread().isAlive() + " 子线程是否存活 "); // true
    }
}

class test_ThreadAndRunableDifference {
    public static void main(String[] args) throws InterruptedException {
        RunableDemo1 demon3 = new RunableDemo1();
        Thread demo3 = new Thread(demon3);
        demo3.start();
        demo3.join();
        System.out.println(demo3.isAlive() + " 子线程是否存活 "); // false
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值