Heim > Java > javaLernprogramm > Wie implementiert man die Inter-Thread-Kommunikation in Java?

Wie implementiert man die Inter-Thread-Kommunikation in Java?

WBOY
Freigeben: 2023-04-20 20:46:09
nach vorne
797 Leute haben es durchsucht

Was ist Thread-Kommunikation und wie wird sie implementiert?

Die sogenannte Thread-Kommunikation besteht darin, Daten zwischen Threads untereinander zu senden. Die Thread-Kommunikation wird normalerweise durch den Austausch von Daten erreicht.

Threads entscheiden anhand der freigegebenen Daten, was zu tun ist, und benachrichtigen andere Threads, was zu tun ist.

Gemeinsame Modelle der Thread-Kommunikation

Produzenten- und Konsumentenmodelle: Produzenten-Threads sind für die Datenproduktion verantwortlich, und Konsumenten-Threads sind für den Datenkonsum verantwortlich.

Anforderung: Nachdem der Produzenten-Thread die Datenproduktion abgeschlossen hat, wecken Sie den Verbraucher und warten Sie dann auf sich selbst. Nachdem der Verbraucher die Daten konsumiert hat, wecken Sie den Produzenten und warten Sie dann auf sich selbst.

public class 多线程_5线程通信 extends Thread{
 
    public static void main(String[] args) {
        //定义线程类,创建一个共享的账户对象
        account3 a=new account3("abc",0);
        //创建两个取钱的线程对象
        new drawthread3(a,"小明").start();
        new drawthread3(a,"小红").start();
        //创建三个存钱的线程对象
        new savethread(a,"存钱罐1号").start();
        new savethread(a,"存钱罐2号").start();
        new savethread(a,"存钱罐3号").start();
    }
}
//存钱的线程类
class savethread extends Thread{
    //接收处理的账户对象
    private account3 acc;
    public savethread(account3 acc,String name){
        super(name);
        this.acc=acc;
    }
    public void run(){
        try {
            while (true){
                //存钱
                acc.savemoney(100000);
                //休眠2秒
                Thread.sleep(2000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
//取钱的线程类
class drawthread3 extends Thread{
    //接收处理的账户对象
    private account3 acc;
    public drawthread3(account3 acc,String name){
        super(name);
        this.acc=acc;
    }
    public void run(){
        try {
            while (true){
                //取钱
                acc.drawmoney3(100000);
                //休眠2秒
                Thread.sleep(2000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class account3{
    private String cartId;
    private double money;//账户余额
 
    public account3() {
    }
 
    public account3(String cartId, double money) {
        this.cartId = cartId;
        this.money = money;
    }
 
    public String getCartId() {
        return cartId;
    }
 
    public void setCartId(String cartId) {
        this.cartId = cartId;
    }
 
    public double getMoney() {
        return money;
    }
 
    public void setMoney(double money) {
        this.money = money;
    }
 
    public synchronized void savemoney(double money) {
        //先获取是谁来存钱,线程名即是人名
        String name=Thread.currentThread().getName();
        //判断账户是否有钱
        try {
            if(this.money==0){
                //没钱,存钱
                this.money+=money;
                System.out.println(name+"来存钱,存了:"+money+"存钱后余额为:"+this.money);
                //有钱了
                //唤醒所有线程
                this.notifyAll();
                //锁对象,让当前线程进入等待
                this.wait();
            }else {
                //有钱,不存钱
                //唤醒所有线程
                this.notifyAll();
                //锁对象,让当前线程进入等待
                this.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public synchronized void drawmoney3(double money) {
        //先获取是谁来取钱,线程名即是人名
        String name=Thread.currentThread().getName();
        try {
            //判断账户是否够钱
            if(this.money>=money){
                //有钱,取钱
                this.money-=money;
                System.out.println(name+"来取钱成功,取了:"+money+"余额是:"+this.money);
                //没钱了
                //唤醒所有线程
                this.notifyAll();
                //锁对象,让当前线程进入等待
                this.wait();
            }else{
                //余额不足
                //唤醒所有线程
                this.notifyAll();
                //锁对象,让当前线程进入等待
                this.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Nach dem Login kopieren

Wie implementiert man die Inter-Thread-Kommunikation in Java?

Das obige ist der detaillierte Inhalt vonWie implementiert man die Inter-Thread-Kommunikation in Java?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:yisu.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage