public static void main(String[] args) {
SellThread st = new SellThread()
Thread th1 = new Thread(st)
th1.start()
Thread th2 = new Thread(st)
th2.start()
Thread th3 = new Thread(st)
th3.start()
}
}
class SellThread implements Runnable {
private int number=10
String s = new String()
public void run() {
while (number >0) {
synchronized (s) {
System.out.println("第" + number + "个人在"
+ Thread.currentThread().getName() + "买票")
}
number--
try {
Thread.sleep(10)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}
}
synchronized (s)的s是synchronized 的参数,synchronized 的参数可以是任意对象,我定义了一个String类型的对象s,方便看程序而已。
但是要注意,s的定义一定要在run方法之外,不然还会出现负数。因为你启动了3个线程,每个线程都调用了run方法,在每个线程就会在run方法里边产生自己的s对象,一共会产生3个,达不到同步的目的。
如果还有不明白的,可以继续问。
interface Wicket {
Object obj = new Object()
public void sellTicket()
public void wicketName()
}
class MovieWicket implements Wicket, Runnable {
int MovieTicket = 30
int Eachticket = 10
public void run() {
for ( Eachticket > 0 Eachticket--)
sellTicket()
}
public synchronized void sellTicket() {
if (MovieTicket > 0 && Eachticket > 0)
System.out.println("MovieTicket剩余" + --MovieTicket + " "
+ Thread.currentThread().getName())
}
public void wicketName() {
}
}
class BusWicket implements Wicket, Runnable {
int BusTicket = 30
int Eachticket = 10
public void run() {
for ( Eachticket > 0 Eachticket--)
sellTicket()
}
public synchronized void sellTicket() {
if (BusTicket > 0 && Eachticket > 0)
System.out.println("BusWicket剩余" + --BusTicket + " "
+ Thread.currentThread().getName())
}
public void wicketName() {
}
}
class TrainWicket implements Wicket, Runnable {
int TrainTicket = 30
int Eachticket = 10
public void run() {
for ( Eachticket > 0 Eachticket--)
sellTicket()
}
public synchronized void sellTicket() {
if (TrainTicket > 0 && Eachticket > 0)
System.out.println("TrainTicket剩余" + --TrainTicket + " "
+ Thread.currentThread().getName())
}
public void wicketName() {
}
}
public class WicketDemo {
static void DoubleThread(Runnable oj) {
Thread t1 = new Thread(oj)
Thread t2 = new Thread(oj)
t1.setName("线程1")
t2.setName("线程2")
t1.start()
t2.start()
}
public static void main(String[] args) {
MovieWicket mw = new MovieWicket()
BusWicket bw = new BusWicket()
TrainWicket tw = new TrainWicket()
while (mw.MovieTicket != 0 || bw.BusTicket != 0 || tw.TrainTicket != 0) {
long t1 = System.currentTimeMillis()
int rand = (int) (Math.random() * 3)
if (rand == 0 && mw.MovieTicket>0) {
System.out.println("正在卖10张电影票!")
WicketDemo.DoubleThread(mw)
mw.Eachticket = 10
} else if (rand == 1 && bw.BusTicket >0) {
System.out.println("正在卖10张汽车票!")
WicketDemo.DoubleThread(bw)
bw.Eachticket = 10
} else if(rand == 2 && tw.TrainTicket >0) {
System.out.println("正在卖10张火车票!")
WicketDemo.DoubleThread(tw)
tw.Eachticket = 10
}
long t2 = System.currentTimeMillis()
if (t2 - t1 < 3000) {
try {
Thread.sleep(3000 - t2 + t1)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}
}
}
刚好我也在学多线程,就拿你的问题练习一下,写出来一坨翔给你参考一下。你的要求应该是已经完全满足了,期待高手优化。
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()
}
}
自己改一下 包名吧,这个题目是很多学校在多线程都用来的基础练习题。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)