「买票系统java」买票系统维护到几点
本篇文章给大家谈谈买票系统java,以及买票系统维护到几点对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、你好,请问一下java模拟5个窗口售票系统你是如何编写的,能教我一下吗?
- 2、java火车票售票用到web吗
- 3、毕业设计java做的网站订票系统要交可执行程序怎么交?
- 4、java 多线程模拟火车售票系统问题
你好,请问一下java模拟5个窗口售票系统你是如何编写的,能教我一下吗?
package yaoshun.Thread;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
/*
* 多线程同步输出
*/
public class MultiThread extends JFrame {
private int ticket; // 计数变量
private JButton jButton;
private JLabel jLabel;
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JTextArea jTextArea1;
private JTextArea jTextArea2;
private JTextArea jTextArea3;
private JTextArea jTextArea4;
private JTextArea jTextArea5;
private Thread thread1;
private Thread thread2;
private Thread thread3;
private Thread thread4;
private Thread thread5;
// 构造器
public MultiThread() {
setName("火车票销售情况");
setLayout(null); // 使用绝对布局
setSize(680, 720);
setResizable(false); // 设置窗体大小不变
setLocationRelativeTo(null);// 设置窗体居中
jLabel = new JLabel("火车站售票大厅");
jLabel.setBounds(280, 20, 140, 30);
jLabel.setFont(new Font(getName(), Font.BOLD, 16)); // 设置字体大小
jLabel.setForeground(Color.BLUE); // 设置字体颜色
jLabel1 = new JLabel("售票窗1");
jLabel1.setBounds(72, 50, 80, 50);
jLabel1.setForeground(Color.red);
jLabel2 = new JLabel("售票窗2");
jLabel2.setBounds(192, 50, 80, 50);
jLabel2.setForeground(Color.red);
jLabel3 = new JLabel("售票窗3");
jLabel3.setBounds(312, 50, 80, 50);
jLabel3.setForeground(Color.red);
;
jLabel4 = new JLabel("售票窗4");
jLabel4.setBounds(432, 50, 80, 50);
jLabel4.setForeground(Color.red);
jLabel5 = new JLabel("售票窗5");
jLabel5.setBounds(552, 50, 80, 50);
jLabel5.setForeground(Color.red);
jTextArea1 = new JTextArea();
jTextArea1.setBounds(45, 100, 100, 500);
jTextArea1.setEditable(false); // 文本框不可编辑
jTextArea2 = new JTextArea();
jTextArea2.setBounds(165, 100, 100, 500);
jTextArea2.setEditable(false); // 文本框不可编辑
jTextArea3 = new JTextArea();
jTextArea3.setBounds(285, 100, 100, 500);
jTextArea3.setEditable(false); // 文本框不可编辑
jTextArea4 = new JTextArea();
jTextArea4.setBounds(405, 100, 100, 500);
jTextArea4.setEditable(false); // 文本框不可编辑
jTextArea5 = new JTextArea();
jTextArea5.setBounds(525, 100, 100, 500);
jTextArea5.setEditable(false); // 文本框不可编辑
jButton = new JButton("开始售票");
jButton.setBounds(270, 625, 140, 40);
jButton.setFont(new Font(getName(), Font.BOLD, 16));// 设置字体
jButton.setForeground(Color.blue);
thread1 = new Thread(new MyThread1());
thread2 = new Thread(new MyThread2());
thread3 = new Thread(new MyThread3());
thread4 = new Thread(new MyThread4());
thread5 = new Thread(new MyThread5());
// 为按钮添加监听器
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
}
});
add(jLabel);
add(jLabel1);
add(jLabel2);
add(jLabel3);
add(jLabel4);
add(jLabel5);
add(jTextArea1);
add(jTextArea2);
add(jTextArea3);
add(jTextArea4);
add(jTextArea5);
add(jButton);
setVisible(true); // 设置窗体显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体关闭
}
class MyThread1 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea1.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class MyThread2 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea2.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class MyThread3 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea3.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class MyThread4 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea4.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class MyThread5 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea5.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
MultiThread multiThread = new MultiThread();
}
}
自己改一下 包名吧,这个题目是很多学校在多线程都用来的基础练习题。
java火车票售票用到web吗
用。用Java Web实现的火车票售票系统,其中实现了火车票售票系统该有的基础功能,是面向演示开发的课程设计。火车票,是乘客乘坐火车需出示的票据,主要由客票和附加票两部分构成。
毕业设计java做的网站订票系统要交可执行程序怎么交?
将Java程序打包成exe可执行文件
第一步:将Java程序通过Eclipse或者JRE导出成Jar包;
第二步:通过exe4j将Jar包程序生成exe可执行文件。
作为毕业设计,要求你交执行程序肯定是有办法实现的啊,当然你也可以用脚本执行,不过既然要交源码,都到这里了打包成exe也就用不了什么时间了
java 多线程模拟火车售票系统问题
在synchronized(str) 里加一句 if(ticket 0) //判断是否有余票,没有余票还卖什么?
也就是这样:
package socket;
public class Test
{
public static void main(String [] args)
{
TestThread tt=new TestThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}
class TestThread implements Runnable
{
int ticket=10;
String str=" ";
public void run()
{
while(ticket0)
{
synchronized(str)
{
if(ticket 0 ) {
System.out.println(Thread.currentThread().getName()+":ticket "+ticket+" is saling");
ticket--;
}
}
}
}
}
你的while循环只是超出票数的循环,在同步里面还要有判断是否有票的条件.因为里面同步的时候除了一个线程在执行外,其它几个都在等待,而此时并没有跳出循环,当然会出现负数了.
当然把while判断放到同步代码里面去也可以.不过这样就没有超出票数的判断了,实际应用中应该有个超出票数的判断.. 例如 :for(int i=0;i100;i++) //同步检测 只有5张票,显示100张,实际上卖出的还是5张..用来检测同步是否正确...
买票系统java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于买票系统维护到几点、买票系统java的信息别忘了在本站进行查找喔。