实验十一 Java多线程程序设计(二)

实验十一  Java多线程程序设计(二)

一、实验目的

  1. 掌握Java多线程程序设计方法。

二、实验内容

上机实现下列程序并观察程序的运行情况:

  1. 用两个线程模拟存票、售票过程。

假定开始售票处并没有票,一个线程往里存票,另外一个线程则往外卖票

新建一个票类对象,让存票和售票线程都访问它。本例采用两个线程共享同一个数据对象来实现对同一份数据的操作。

三、实验要求(必做实验,2学时)

  1. 掌握Java多线程的创建和使用。
  2. 掌握Java多线程同步技术;理解产生死锁现象的原因,学习如何避免死锁。

四、实验结果、发现和总结

package synch;



import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;



/**

 * @program: Java_Experiment

 * @description:

 * @author: SXH

 * @create: 2022-04-21 11:23

 **/

public class Ticket {

    private Lock lock;

    private Condition condition;

    private int number;



    public Ticket(){

        number=0;

        lock=new ReentrantLock();

        condition= lock.newCondition();

    }

    public void transfer(int key,int n) throws InterruptedException{

        lock.lock();

        int judge=(int)Math.pow(-1,key);

        try {

            while(number+judge*n<0)

                condition.await();

            System.out.print(Thread.currentThread());

            number+=judge*n;

            if (judge==-1) System.out.printf(" now take %d tickets out, ",n);

            else System.out.printf(" now add %d tickets, ",n);

            System.out.printf("current number is: %d\n",number);

            condition.signal();

        }

        finally {

            lock.unlock();

        }

    }

}
import synch.Ticket;

/**

 * @program: Java_Experiment

 * @description: Java Experience_11 use Thread and Runnable to simulate selling tickets

 * @author: SXH

 * @create: 2022-04-21 10:30

 **/

public class SellTickets {

    public static void main(String[] args) {

        var ticket=new Ticket();

            Runnable t=()->{

                try {

                    while (true) {

                        int key = 1;

                        int n = (int) (Math.random() * 10);

                        ticket.transfer(key, n);

                        Thread.sleep((int) (Math.random() * 10));

                    }

                }

                catch (InterruptedException e){

                    System.out.println(e.getMessage());

                }

            };

            Runnable a=()->{

                try {

                    while (true) {

                        int key = 0;

                        int n = (int) (Math.random() * 10);

                        ticket.transfer(key, n);

                        Thread.sleep((int) (Math.random() * 10));

                    }

                }

                catch (InterruptedException e){

                    System.out.println(e.getMessage());

                }

            };

            var take=new Thread(t);

            var add=new Thread(a);

            take.start();

            add.start();

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1125rx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值