Java多线程

Java多线程

  • 线程是系统中的最小执行单元,同一个进程中有多个线程,线程共享进程的资源
  • 进程持有资源(内存)和线程
  • 线程的交互:互斥(竞争)和同步(协作)

Java对多线程的支持:Thread类和Runnable接口,都包含在java.long包中,有个共同的方法run() 在这里插入图片描述
在这里插入图片描述

package Thred;

public class Actor extends Thread {
    public static void main(String[] args){
        Thread actor =new Actor();
        actor.setName("Mr.Thread");
        actor.start();

        Thread actressThread = new Thread(new Actress(),"Ms.Runnable");
        actressThread.start();
    }

    public void run(){
        System.out.println(getName()+"是一个演员");
        int count= 0;
        boolean c = true;
        while (c){
            System.out.println(getName()+"登台演出:"+(count++));
            if(count==100){
                c=false;
            }
            if(count%10==0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }


        System.out.println(getName()+"演出结束了");
    }



}
class  Actress implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"是一个演员");
        int count= 0;
        boolean c = true;
        while (c){
            System.out.println(Thread.currentThread().getName()+"登台演出:"+(count++));
            if(count==100){
                c=false;
            }
            if(count%10==0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }


        System.out.println(Thread.currentThread().getName()+"演出结束了");

    }
}

在这里插入图片描述
通过运行结果可看出,当一个线程休眠了另外一个线程启动
例:
军队线程:

package Thred;
/*
军队线程,模拟作战双方的行为
 */
public class ArmyRunnable implements Runnable {
    //volatile保证了线程可以正确的读取其他线程写入的值
    //可见性
    volatile boolean keepRunning=true;
    @Override
    public void run() {
        while(keepRunning){
            //发动5连击
            for(int i=0;i<5;i++){
                System.out.println(Thread.currentThread().getName()+"进攻对方"+i+"次");
                //让出了处理器时间,下次该谁进攻还不一定呢
                Thread.yield();
            }
        }
        System.out.println(Thread.currentThread().getName()+"攻击结束");
    }
}

关键人物:

package Thred;

public class keyPerson extends Thread {
    public void run(){
        System.out.println(Thread.currentThread().getName()+"开始战斗");
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+"冲杀隋军666666666");
        }


        System.out.println(Thread.currentThread().getName()+"结束战斗");
    }
}

舞台线程:

package Thred;
/*
隋唐演义的大戏舞台
 */
public class stage extends Thread{
    public static void main(String[] args){
        new stage().start();
    }

    //重写run()
    public void run(){
        System.out.println("大戏开始");
        //让观众安静下来
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ArmyRunnable sui =new ArmyRunnable();
        ArmyRunnable nong = new ArmyRunnable();

        //用Runnable接口创建线程
        Thread suiXC = new Thread(sui,"隋军");
        Thread nongXC = new Thread(nong,"农民军");

        //启动线程,开始作战
        suiXC.start();
        nongXC.start();

        //舞台线程休眠,大家专心观看军队厮杀
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        sui.keepRunning=false;
        nong.keepRunning=false;

        try {
            nongXC.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("半路杀出个程咬金");
        Thread mrCheng =new keyPerson();
        mrCheng.setName("程咬金");
        System.out.println("他的梦想是结束战争");
        //停止军队作战(停止线程的方法)
        sui.keepRunning=false;
        nong.keepRunning=false;

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        mrCheng.start();

        try {
            mrCheng.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("战争结束");
        System.out.println("大戏结束");

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值