什么都不会的学习之路——Java多线程

什么都不会的学习之路——Java多线程,第1张

多线程 启动一个线程 创建多线程-继承线程类

使用多线程,就可以做到多件事情同时发生
1.设计一个类KillThread 继承Thread,并且重写run方法
2.启动线程办法: 实例化一个KillThread对象,并且调用其start方法

public class TestThread {
    public static void main(String[] args) {
        Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.life = 616;
        gareen.harm = 50;

        Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.life = 300;
        teemo.harm = 30;

        Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.life = 500;
        bh.harm = 65;

        Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.life = 455;
        leesin.harm = 80;

        KillThread killThread1 = new KillThread(gareen,teemo);
        killThread1.start();
        KillThread killThread2 = new KillThread(bh,leesin);
        killThread2.start();
    }
}

class Hero {
    public String name;
    public float life;
    public int harm;

    public void attackHero(Hero h) throws InterruptedException {
        Thread.sleep(1000);
        h.life-=harm;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.life);
        if(h.isDead())
            System.out.println(h.name+"死了!");
    }
    public boolean isDead() {
        return 0>=life?true:false;
    }
}

class KillThread extends Thread{
    private Hero h1;
    private Hero h2;

    public KillThread(Hero h1, Hero h2) {
        this.h1 = h1;
        this.h2 = h2;
    }

    @Override
    public void run() {
        while(!h2.isDead()){
            try {
                h1.attackHero(h2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

创建多线程-实现Runnable接口

创建类Battle,实现Runnable接口
启动的时候,首先创建一个Battle对象,然后再根据该battle对象创建一个线程对象,并启动

class Hero {
    public String name;
    public float life;
    public int harm;

    public void attackHero(Hero h) throws InterruptedException {
        Thread.sleep(1000);
        h.life-=harm;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.life);
        if(h.isDead())
            System.out.println(h.name+"死了!");
    }
    public boolean isDead() {
        return 0>=life?true:false;
    }
}
public class testThead {
    public static void main(String[] args) {
        Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.life = 616;
        gareen.harm = 50;

        Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.life = 300;
        teemo.harm = 30;

        Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.life = 500;
        bh.harm = 65;

        Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.life = 455;
        leesin.harm = 80;

        Battle battle1 = new Battle(gareen,teemo);
        new Thread(battle1).start();
        Battle battle2 = new Battle(bh,leesin);
        new Thread(battle2).start();
    }
}
class Battle implements Runnable{
    private Hero h1;
    private Hero h2;
 
    public Battle(Hero h1, Hero h2){
        this.h1 = h1;
        this.h2 = h2;
    }
    public void run(){
        while(!h2.isDead()){
            h1.attackHero(h2);
        }
    }
}

创建多线程-匿名类 常见的线程方法 1.sleep

当前线程暂停

Thread.sleep(1000); 
2.join

加入到当前线程中

执行t.join,即表明在主线程中加入该线程。
主线程会等待该线程结束完毕, 才会往下运行。

t.join();
3.setPriority

线程优先级

t.setPriority();
4.yield

临时暂停

Thread.yield();
5.setDaemon

守护线程

当一个进程里,所有的线程都是守护线程的时候,结束当前进程。

t.setDaemon(true);
同步

Thread.currentThread().getName() 指 当前调用方法的线程对象

public class Bank {
    public static void main(String[] args) {
        Account account = new Account("结婚基金",100);
        Drawing gwj = new Drawing(account,100,"葛文婧");
        Drawing ztb = new Drawing(account,50,"张天保");

        gwj.start();
        ztb.start();
    }
}
//账户
class Account{
    String name;
    int money;

    public Account(String name, int money) {
        this.name = name;
        this.money = money;
    }
}
//模拟取钱
class Drawing extends Thread{
    Account account;
    int drawMoney;
    int nowMoney;

    public Drawing(Account account, int drawMoney, String name) {
        super(name);
        this.account = account;
        this.drawMoney = drawMoney;

    }
    public void run(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if((account.money-drawMoney)<nowMoney){
            System.out.println(Thread.currentThread().getName()+"钱不够,取不了");
            return;
        }
        account.money = account.money-drawMoney;
        nowMoney = nowMoney+drawMoney;
        System.out.println(account.name+"余额为"+account.money);
        System.out.println(this.getName()+"get"+nowMoney);
    }
}
死锁 synchronized ()
package 多线程demo03;

public class DeadLock {
    public static void main(String[] args) {
        people ztb = new people(0,"张天保");
        people gwj = new people(1,"葛文婧");

        ztb.start();
        gwj.start();

    }
}
//口红
class kh{

}
//镜子
class jz{

}
//化妆的人
class people extends Thread{
    static kh k = new kh();
    static jz j = new jz();

    int choic;
    String name;

    public people(int choic, String name) {
        this.choic = choic;
        this.name = name;
    }
    public void run(){
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private void makeup() throws InterruptedException {
        if (choic == 0) {
            synchronized (j) {
                System.out.println(this.name + "获得镜子1");
                Thread.sleep(1000);
            }
            synchronized (k) {
                System.out.println(this.name + "获得口红1");
            }
        }else {
            synchronized (k) {
                System.out.println(this.name + "获得口红");
                Thread.sleep(1000);
            }
            synchronized (j) {
                System.out.println(this.name + "获得镜子");
            }
        }
    }
}

Lock锁
private final ReentrantLock lock = new ReentrantLock();
lock.lock();
lock.unlock();

intln(this.name + “获得口红”);
Thread.sleep(1000);
}
synchronized (j) {
System.out.println(this.name + “获得镜子”);
}
}
}
}




## Lock锁

```java
private final ReentrantLock lock = new ReentrantLock();
lock.lock();
lock.unlock();

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/724911.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存