多线程基础二(java)

Runnable

创建两个线程

package Threading;

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

//测试
package Threading;

public class test {
    public static void main(String[] args) {
        Myrunable my = new Myrunable();
        Thread t1 = new Thread(my,"飞机");
        Thread t2 = new Thread(my,"火车");
        t1.start();
        t2.start();

    }
}

卖票案例

//存在安全问题的
package SellTicket;

public class SellTicket implements Runnable{
    private int ticket = 100;

    @Override
    public void run() {
        while(true){
            if(ticket>0){
                System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
                ticket--;
            }
        }
    }
}

package SellTicket;

public class test {
    public static void main(String[] args) {
        SellTicket st = new SellTicket();
        Thread t1 = new Thread(st,"窗口1");
        Thread t2 = new Thread(st,"窗口2");
        Thread t3 = new Thread(st,"窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}

//改进之后的
package SellTicket;

public class SellTicket implements Runnable{
    private int ticket = 100;
    private Object obj =new Object();

    @Override
    public void run() {
        while(true){
            synchronized (obj){
                if(ticket>0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
                    ticket--;

                }
            }
        }
    }
}

在这里插入图片描述

Lock

package SellTicket;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SellTicket implements Runnable{
    private int ticket = 100;
    private Lock lock = new ReentrantLock();


    @Override
    public void run() {
        while(true){
            try{
                lock.lock();
                if(ticket>0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
                    ticket--;

                }
            }finally {
                lock.unlock();
            }
        }
    }
}

生产者消费者问题

package prosellquestion;

public class Box {
    private int milk;
    private boolean status = false;


    public synchronized void put(int milk){
        //如果有牛奶,等待消费
        if(status){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //生产
        this.milk=milk;
        System.out.println("生产者生产了第"+this.milk+"瓶奶");

        //生产完之后修改盒子中的信息
        status=true;
        //唤醒
        notifyAll();

    }
    public synchronized void get(){
        //如果没有牛奶,则等待
        if(!status){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //消费
        System.out.println("消费者拿到了第"+this.milk+"瓶奶");
        //取走牛奶之后,修改信息
        status=false;
        //唤醒
        notifyAll();
    }
}

package prosellquestion;

public class producer implements Runnable{
    private Box box;
    public producer(Box box) {
        this.box=box;
    }

    @Override
    public void run() {
        while (true){
            box.get();
        }
    }
}

package prosellquestion;

public class seller implements Runnable{
    private Box box;
    public seller(Box box) {
        this.box= box;
    }

    @Override
    public void run() {
        for(int i=1;i<50;i++){
            box.put(i);
        }

    }
}

package prosellquestion;

public class test {
    public static void main(String[] args) {
        Box box = new Box();
        producer p = new producer(box);
        seller s = new seller(box);

        Thread p1 = new Thread(p,"生产者");
        Thread s1 = new Thread(s,"消费者");
        p1.start();
        s1.start();

    }
}

生产者消费者改进

package prosellquestion;
import java.util.Random;
public class Box {
    private int lenght = 0;
    private static int bufferlenght=50;
    private boolean status = false;
    Random ra = new Random();

    public synchronized void put(){
        //如果有牛奶,等待消费
        int r=ra.nextInt(bufferlenght);
        int orlen=0;
        if(status||lenght>=bufferlenght){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //生产
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(lenght+r>bufferlenght){
            orlen=bufferlenght-lenght;
            lenght=bufferlenght;
            System.out.println("生产者生产了"+r+"个文件,但超出缓冲区,只存入"+orlen+"个文件,"+"缓冲区中还剩"+lenght);
        }
        else {
            lenght+=r;
            System.out.println("生产者生产了"+r+"个文件,还剩"+lenght);
        }
        //生产完之后修改盒子中的信息
        status=true;
        //唤醒
        notify();
    }
    public synchronized void get(){
        int r=ra.nextInt(bufferlenght);
        int orlen=0;
        //如果没有牛奶,则等待
        if(!status||lenght<=0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //消费
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //判断缓冲区中是否有这么多文件
        if(lenght-r<0){
           orlen = lenght;
            lenght=0;
            System.out.println("消费者消费"+r+"个文件,但缓冲区中文件不足,只消费了"+orlen+"个文件,"+"缓冲区中还剩"+lenght);
        }
        else {
            lenght-=r;
            System.out.println("消费者消费了"+r+"个文件,还剩"+lenght);
        }
        //取走牛奶之后,修改信息
        status=false;
        //唤醒
        notify();
    }
}

package prosellquestion;

public class seller implements Runnable{
    private Box box;
    public seller(Box box) {
        this.box= box;
    }

    @Override
    public void run() {
        while (true){
            box.put();
        }
    }
}

package prosellquestion;

public class producer implements Runnable{
    private Box box;
    public producer(Box box) {
        this.box=box;
    }

    @Override
    public void run() {
        while (true){
            box.get();
        }

    }
}

package prosellquestion;

public class test {
    public static void main(String[] args) {
        Box box = new Box();
        producer p = new producer(box);
        seller s = new seller(box);

        Thread p1 = new Thread(p,"生产者");
        Thread s1 = new Thread(s,"消费者");
        p1.start();
        s1.start();
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值