java多线程2

在java中多线程并不会继承Thread,因为java中只有单继承,如果继承了Thread类就不可以继承其他类了,所以多用实现Runnable来实现,这样不仅可以继承还可以实现其他类。

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "------->" + i);
        }
    }
}

但是Runable中没有start方法,不可以开一个线程,所以我们还是要用Thread来开线程。

我们可以通过Thread.sleep()的方法来使一个线程进入阻塞状态,可以让其他的线程先运行

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "------->" + i);
        }
    }
}

这样就可以让main线程先运行一秒,然后再并发运行,运行结果如下

main------->0
main------->1
main------->2
main------->3
main------->4
main------->5
main------->6
main------->7
main------->8
main------->9
MyRunnable------->0
MyRunnable------->1
MyRunnable------->2
MyRunnable------->3
MyRunnable------->4
MyRunnable------->5
MyRunnable------->6
MyRunnable------->7
MyRunnable------->8
MyRunnable------->9

Process finished with exit code 0

如果想要在某时刻让他停止睡眠可以用interrupt()方法来停止

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(1000*5);
        } catch (InterruptedException e) {
            e.getStackTrace();
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "------->" + i);
        }
    }
}
public class Main {
    public static void main(String[] args) {
        MyRunnable rb = new MyRunnable();
        Thread t = new Thread(rb,"MyRunnable");
        t.start();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "------->" + i);
            if(i == 3){
                t.interrupt();
            }
        }
    }
}

现在的运行结果

main------->0
main------->1
main------->2
main------->3
MyRunnable------->0
MyRunnable------->1
MyRunnable------->2
MyRunnable------->3
MyRunnable------->4
MyRunnable------->5
MyRunnable------->6
MyRunnable------->7
MyRunnable------->8
MyRunnable------->9
main------->4
main------->5
main------->6
main------->7
main------->8
main------->9

Process finished with exit code 0

MyRunnable本来要睡眠5秒的,但是当main线程中i等于3是打断了它的睡眠使它开始运行,由于main每次要睡眠一秒,再这一秒中MyRunnable运行完了。

还有打标志的方法

public class MyRunnable implements Runnable{
    boolean b = true;
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            if(b) {
                try {
                    Thread.sleep(1000 * 5);
                } catch (InterruptedException e) {
                    e.getStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "------->" + i);
        }
    }
}
public class Main {
    public static void main(String[] args) {
        MyRunnable rb = new MyRunnable();
        Thread t = new Thread(rb,"MyRunnable");
        t.start();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "------->" + i);
            if(i == 3){
                rb.b=false;
            }
        }
    }
}

运行结果

main------->0
main------->1
main------->2
main------->3
MyRunnable------->0
MyRunnable------->1
MyRunnable------->2
MyRunnable------->3
MyRunnable------->4
MyRunnable------->5
MyRunnable------->6
MyRunnable------->7
MyRunnable------->8
MyRunnable------->9
main------->4
main------->5
main------->6
main------->7
main------->8
main------->9

Process finished with exit code 0

当main线程到3的时候b等于false,MyRunnable不会进入睡眠。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值