线程间共享数据例子--售票/退票

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!


例子程序:

package edu.review;

import java.util.Random;
/**
 * 题目要求:
 *   两个售票窗口,对某一趟列车票进行操作,一个为售票窗口,一个为退票窗口,每个窗口售/退票100张 
 **/
public class Thread2ThreadDataShare {

	public static void main(String[] args) {
		// 方式1();//两种方式是等效的
		方式2();

	}
	
	public static void 方式1(){
		final DataShare data1 = new DataShare();
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 100; i++) {
					try {
						Thread.sleep(new Random().nextInt(20));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					data1.increment();
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 100; i++) {
					try {
						Thread.sleep(new Random().nextInt(20));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					data1.decrement();
				}
				
			}
		}).start();
	}
	
	public static void 方式2(){
		DataShare data2 = new DataShare();
		IncRunnable incRunnable = new IncRunnable(data2);
		DecRunnable decRunnable = new DecRunnable(data2);
		new Thread(incRunnable).start();
		new Thread(decRunnable).start();
	}
	static class IncRunnable implements Runnable{
		DataShare data = new DataShare();
		public IncRunnable(DataShare data){
			this.data = data;
		}
		@Override
		public void run() {
			for (int i = 0; i < 100; i++) {
				try {
					Thread.sleep(new Random().nextInt(20));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				data.increment();
			}
		}
	}
	static class DecRunnable implements Runnable{
		DataShare data = new DataShare();
		public DecRunnable(DataShare data){
			this.data = data;
		}
		@Override
		public void run() {
			for (int i = 0; i < 100; i++) {
				try {
					Thread.sleep(new Random().nextInt(20));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				data.decrement();
			}
		}
	}

}

class DataShare{
	private int ticket = 100;
	public synchronized void increment(){
		ticket++;
		System.out.println(Thread.currentThread().getName()+"退票,当前剩"+ticket+"张票!");
	}
	public synchronized void decrement(){
		ticket--;
		System.out.println(Thread.currentThread().getName()+"售票,当前剩"+ticket+"张票!");
	}
	
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 第一章. 概述 1 1.1概述 1 1.2意义 1 1.3任务 1 第二章. 系统的可行性研究与需求分析 2 2.1可行性研究 2 2.1.1经济可行性 2 2.1.2技术可行性 2 2.1.3操作可行性 2 2.2需求分析 2 2.2.1功能需求 2 2.2.2数据需求 3 2.2.3性能需求 3 2.2.4数据库逻辑结构 6 第三章. 系统的总体设计 7 3.1系统软件结构设计 7 3.1.1软件结构 7 3.2系统流程图 9 第四章. 系统的详细设计 10 4.1.1程序流程图 11 第五章. 系统的实现与调试 18 5.1应用系统的开发及测试 18 5.1.1系统首页 18 5.1.2用户登录及访问权限 19 5.1.3车次信息查询 21 5.1.4售票 21 5.15退票 22 结束语 23 致谢.....................................................................24 参考文献 25 附录A...............................................................................26附录B...............................................................................30 附录C............................................................................. 32 附录 登陆窗 #region Windows 窗体设计器生成的代码 private void InitializeComponent() { this.lblID = new System.Windows.Forms.Label(); this.lblPassWord = new System.Windows.Forms.Label(); this.cbSelect = new System.Windows.Forms.ComboBox(); this.lblSelect = new System.Windows.Forms.Label(); this.txtID = new System.Windows.Forms.TextBox(); this.txtPassWord = new System.Windows.Forms.TextBox(); this.btnCancel = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.skinEngine1 = new Sunisoft.IrisSkin.SkinEngine(((System.ComponentModel.Component)(this))); this.btnEnter = new System.Windows.Forms.Button(); this.SuspendLayout(); // LoginForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoValidate = System.Windows.Forms.AutoValidate.EnablePreventFocusChange; this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.ClientSize = new System.Drawing.Size(322, 312); this.Controls.Add(this.label1); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnEnter); this.Controls.Add(this.txtPassWord); this.Controls.Add(this.txtID); this.Controls.Add(this.lblSelect); this.Controls.Add(this.cbSelect); this.Controls.Add(this.lblPassWord); this.Controls.Add(this.lblID); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.MaximumSize = new System.Drawing.Size(332, 348); this.MinimumSize = new System.Drawing.Size(332, 348); this.Name = "LoginForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "登录界面"; this.Load += new System.EventHandler(this.Login_Load); this.ResumeLayout(false); this.PerformLayout(); } } } 附录B 主界面 namespace TicketMana { partial class SellerForm { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; namespace TicketMana { partial class SellTicketForm { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值