【Java学习笔记】多线程

教程

1 线程的概念

线程为进程的一部分,一个进程中可同时触发多个线程,执行不同的任务

2 创建多线程的方式

2.1 继承Thread

package charactor;

public class HpPluse extends Thread{
   public Hero h1;

   public HpPluse(Hero hero){
       this.h1 = hero;
   }

   public void run(){
       while(!h1.hpMax()){
           h1.hpPulse();
       }
   }
   
}

package charactor;

public class TestHpPluse {
    public static void main(String[] args){
        Hero h1 = new Hero("一号选手",2,3);
        Hero h2 = new Hero("二号选手",9,8);

        HpPluse hpp1 = new HpPluse(h1);
        hpp1.start();
        HpPluse hpp2 = new HpPluse(h2);
        hpp2.start();
    }
}

2.2 实现Runnable接口

package charactor;

public class Battle implements Runnable {
    public Hero h1;

    public Battle(Hero hero){
        this.h1 = hero;
    }

    public void run(){
        while(!h1.hpMax()){
            h1.hpPulse();
        }
    }
}

package charactor;


public class BattleTest {
    public static void main(String[] args){
        Hero h1 = new Hero("一号选手",2,3);
        Hero h2 = new Hero("二号选手",9,8);

        Battle ba1 = new Battle(h1);
        Battle ba2 = new Battle(h2);

        new Thread(ba1).start();
        new Thread(ba2).start();
    }
}

3 常见的线程方法

在这里插入图片描述

3.1 sleep 当前线程暂停

package threadmethod.sleep;

public class SimpleTimer {
    public static void main(String[] args){
        int sum = 10;

        while(true){
            System.out.println("sum"+sum);
            sum--;

            if(sum<0){
                break;
            }

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


        }
        System.out.println("done!");
    }
}

3.3 setPriority 线程优先级

package threadmethod.priority;

public class SubThread extends Thread {
    @Override
    public void run(){
        long begin = System.currentTimeMillis();
        long sum = 0;
        for (int i = 0; i < 100000000; i++) {
            sum = sum+i;
            this.yield();
        }
        long end = System.currentTimeMillis();
        long time = end-begin;
        System.out.println(this.getName()+"用时:"+time);

    }
}


package threadmethod.priority;

public class Test {
    public static void main(String[] args){
        SubThread sb1 = new SubThread();
        sb1.setPriority(Thread.MAX_PRIORITY);
        sb1.setName("优先级max线程");
        SubThread sb2 = new SubThread();
        sb2.setPriority(Thread.MIN_PRIORITY);
        sb2.setName("优先级min线程");

        sb1.start();
        sb2.start();
    }
}

/*
输出
优先级max线程用时:8692
优先级min线程用时:10378
 */

3.4 yield 临时暂停

package threadmethod.yeild;

public class SubThread extends Thread {
    @Override
    public void run(){
        long begin = System.currentTimeMillis();
        long sum = 0;
        for (int i = 0; i < 100000000; i++) {
            sum = sum+i;
        this.yield();
        }
        long end = System.currentTimeMillis();
        long time = end-begin;
        System.out.println("用时:"+time);

    }
}

package threadmethod.yeild;

public class Test {
    public static void main(String[] args){
        SubThread sb1 = new SubThread();
        sb1.start();

        long begin = System.currentTimeMillis();
        long sum = 0;
        for (int i = 0; i < 100000000; i++) {
            sum = sum+i;
        }
        long end = System.currentTimeMillis();
        long time = end-begin;
        System.out.println("main用时:"+time);



    }
}
/*
输出
main用时:35
用时:8470
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值