Java实验七(下):多线程

实验目的

熟悉多线程的基本操作以及掌握实现多线程的两种方式,同步机制以及线程间通信

实验内容

1、龟兔赛跑,龟兔在进行赛跑,兔子每步跑5米,乌龟每步跑1米,兔子每跑1步,休息1秒钟,乌龟每跑1步,休息0.2秒。乌龟或兔子谁先跑到50米,谁就胜出
要求如下:
采用多线程方式来实现龟兔赛跑;
修改参数,如龟兔休息的时间、步宽等,看看对最终的结果是否有影响。

实验代码

package experiment_seven;
//较project7更加优化
class RunNer extends Thread{
	String name;
	int way;
	int times;
	int distance;
	static boolean flag = true;
	public RunNer(String name,int way,int times,int distance){
		this.name = name;
		this.way = way;
		this.times = times;
		this.distance = distance;
		
	}
	public void run() {
		while(flag) {
				distance = distance - way;
				System.out.println(name + "剩余" + distance +"米");
			if(distance==0) {
				flag = false;
				System.out.println(name + "先到达终点");
				return;
			}
				
			try {
				Thread.sleep(times);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
public class project7_1 {
	public static void main(String[] args) {
		int distance = 50;
		RunNer wugui = new RunNer("乌龟",1,200,distance);
		RunNer tuzi = new RunNer("兔子",5,1000,distance);
		wugui.start();
		tuzi.start();
			
	}
}

实验内容

2、实现生产消费模式。假设有2个生产者,3个消费者,生产者负责生产商品,消费者负责购买。如果商品数量为0,则消费者暂停购买,直到有商品了才能继续,每个生产者最多生产10个商品
要求如下:
采用多线程方式来实现
可能用到的方法 wait();notify()。

实验代码

package experiment_seven;
class Product{
    private int productCount = 0;
    //生产产品
    public synchronized void produceProduct() {
        if(productCount < 10){
            productCount++;
            System.out.println(Thread.currentThread().getName() + ":开始生产第" + productCount + "个产品");
            notify();
        }else{
            //等待
            try {
                wait(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    //消费产品
    public synchronized void consumeProduct() {
        if(productCount > 0){
            System.out.println(Thread.currentThread().getName() + ":开始消费第" + productCount + "个产品");
            productCount--;
            notify();
        }else{
            //等待
            try {
                wait(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Producer extends Thread{//生产者

    private Product product;

    public Producer(Product product) {
        this.product = product;
    }

    @Override
    public void run() {
        //System.out.println(getName() + ":开始生产产品.....");
        int a = 10;
        while(a!=0){
        	 try {
                 Thread.sleep(10);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
            product.produceProduct();
            a--;
        }
    }
}

class Consumer extends Thread{//消费者
    private Product product;

    public Consumer(Product product) {
        this.product = product;
    }

    @Override
    public void run() {
        //System.out.println(getName() + ":开始消费产品.....");
        int a = 10;
        while(a!=0){

            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            product.consumeProduct();
            a--;
        }

    }
}

public class project_7_2_new {

    public static void main(String[] args) {
        Product product = new Product();

        Producer p1 = new Producer(product);
        p1.setName("生产者1");
        Producer p2 = new Producer(product);
        p2.setName("生产者2");
        Consumer c1 = new Consumer(product);
        c1.setName("消费者1");
        Consumer c2 = new Consumer(product);
        c2.setName("消费者2");
        Consumer c3 = new Consumer(product);
        c3.setName("消费者3");
        p1.start();
        p2.start();
        c1.start();
        c2.start();
        c3.start();
    }
}

1.腾龙梦屋文章内容无特殊注明皆为源儿原创,转载请注明来源,谢谢!
2.若有相关文章侵犯您的权益,请联系源儿删除,谢谢!
3.相关软件、资料仅供学习参考使用,在24h内务必删除!
腾龙梦屋 » Java实验七(下):多线程
加速支持