请教高手,一个线程加上队列的题目?

BlueBirdssh 2005-11-23 09:04:09
有一队列长度为10,一个单元只有一个元素且为整形,用线程实现消费者在该队列头取数据,生产者在该队列尾增加数据. 请给出实现的流程或思想,有代码例子更好.
...全文
113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlueBirdssh 2005-11-23
  • 打赏
  • 举报
回复
自己顶一下,本人新学的,希望有简洁思路或代码,谢谢各位!
yguangv 2005-11-23
  • 打赏
  • 举报
回复
刚好最近作了一个练习
可以参考一下,原理大概就是这样

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class ThreadMain implements ActionListener{
private JTextArea info = null;
private JFrame frame = null;
private JButton pause = null;
private JButton resume = null;
private JButton stop = null;
private JButton restart = null;
private ThreadInfo ti = null;
private ThreadAdd ta = null;
private ThreadDel td = null;

public static void main(String[] args) {
ThreadMain tm = new ThreadMain();
tm.init();
}

private void init() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception e) {
e.printStackTrace();
}

frame = new JFrame("Thread Test");
int width = (int)frame.getToolkit().getScreenSize().getWidth();
int height = (int)frame.getToolkit().getScreenSize().getHeight();
frame.setSize(width / 2, height / 2);
frame.setLocation(width / 4, height / 4);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel controlPanel = new JPanel();
frame.getContentPane().add(controlPanel, java.awt.BorderLayout.NORTH);
pause = new JButton("Pause");
pause.addActionListener(this);
controlPanel.add(pause);
resume = new JButton("Resume");
resume.setEnabled(false);
resume.addActionListener(this);
controlPanel.add(resume);
stop = new JButton("Stop");
stop.addActionListener(this);
controlPanel.add(stop);
restart = new JButton("Restart");
restart.addActionListener(this);
controlPanel.add(restart);

info = new JTextArea();
info.setEditable(false);
JScrollPane infoPanel = new JScrollPane(info);
frame.getContentPane().add(infoPanel, java.awt.BorderLayout.CENTER);
frame.setVisible(true);

ti = new ThreadInfo(this);
initThread();
}

public void initThread() {
ta = new ThreadAdd(ti);
ta.start();
td = new ThreadDel(ti);
td.start();
}

public void setInfo(String runningInfo) {
info.append(runningInfo);
}

public void disableBtn() {
pause.setEnabled(false);
resume.setEnabled(false);
stop.setEnabled(false);
}

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(pause.getText())) {
ti.pause();
pause.setEnabled(false);
resume.setEnabled(true);
} else if(e.getActionCommand().equals(resume.getText())) {
ti.resume();
pause.setEnabled(true);
resume.setEnabled(false);
} else if(e.getActionCommand().equals(stop.getText())) {
ti.stop();
pause.setEnabled(false);
resume.setEnabled(false);
stop.setEnabled(false);
} else if(e.getActionCommand().equals(restart.getText())) {
ti.restart();
pause.setEnabled(true);
resume.setEnabled(false);
stop.setEnabled(true);
}
}
}

------------------------------------------------------------
import java.util.*;

public class ThreadInfo {
//memory size
private static final int SIZE = 10;
//running flag
public static final int RUN = 0;
public static final int PAUSE = 1;
public static final int STOP = -1;
//memory
private static List memory = null;
//memory flag
private static int flag = 0;
//running infomation
private String runningInfo = null;
//ThreadMain
private ThreadMain tm = null;

ThreadInfo(ThreadMain tm) {
this.tm = tm;
memory = new ArrayList();
initMemory();
}

/**
* add object to memory
* @param obj(Object)
*/
public synchronized int add(Object obj) {
if(flag == RUN) {
synchronized(memory) {
if(memory.size() < SIZE) {
runningInfo = ">>Add To Memory: " + obj + "\n";
memory.add(obj);
} else {
flag = STOP;
tm.disableBtn();
runningInfo = "[ Memory is Full ]\n";
runningInfo += "[ Threads Stopped ]\n";
}
}
tm.setInfo(runningInfo);
}
return flag;
}

/**
* delete object from memory
* @param obj(Object)
*/
public synchronized int del() {
if(flag == RUN) {
synchronized(memory) {
if(memory.size() > 0) {
runningInfo = "<<Delete From Memory: " + memory.get(0) + "\n";
memory.remove(0);
} else {
flag = STOP;
tm.disableBtn();
runningInfo = "[ Memory is Empty ]\n";
runningInfo += "[ Threads Stopped ]\n";
}
tm.setInfo(runningInfo);
}
}
return flag;
}

public void initMemory() {
memory.clear();
Random r = new Random();
for(int i = 0; i < 5; i++) {
memory.add(new Long(r.nextInt(100)));
}
runningInfo = "========================================\n";
runningInfo += "[ Initialize Memory ]\n" + getMemoryInfo() + "[ Threads Begin ]\n";
tm.setInfo(runningInfo);
}

public String getMemoryInfo() {
String memoryInfo = "";
for(int i = 0; i < memory.size(); i++) {
memoryInfo += "-- " + memory.get(i).toString() + "\n";
}
return memoryInfo;
}

public String getInfo() {
return runningInfo;
}
public void pause() {
flag = PAUSE;
runningInfo = "[ Threads Paused ]\n";
tm.setInfo(runningInfo);
}
public void resume() {
flag = RUN;
runningInfo = "[ Threads Resumed ]\n";
tm.setInfo(runningInfo);
}
public void stop() {
flag = STOP;
runningInfo = "[ Threads Stopped ]\n";
tm.setInfo(runningInfo);
}
public void restart() {
runningInfo = "[ Threads Restart ]\n";
tm.setInfo(runningInfo);
this.initMemory();
flag = RUN;
}
}
-------------------------------------------------------------
import java.util.Random;

public class ThreadAdd extends Thread {
private ThreadInfo ti = null;

ThreadAdd(ThreadInfo info) {
super();
ti = info;
}

public void run() {
Random r = new Random();
while(true) {
try {
sleep(r.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
Integer obj = new Integer(r.nextInt(100));
ti.add(obj);
/*if(ti.add(obj) == ThreadInfo.STOP) {
break;
}*/
}
}
}
------------------------------------------------------------------------
import java.util.Random;

public class ThreadDel extends Thread {
private ThreadInfo ti = null;

ThreadDel(ThreadInfo info) {
super();
ti = info;
}

public void run() {
Random r = new Random();
while(true) {
try {
sleep(r.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
ti.del();
/*if(ti.del() == ThreadInfo.STOP) {
break;
}*/
}
}
}
treeroot 2005-11-23
  • 打赏
  • 举报
回复
so normal!
Hylas 2005-11-23
  • 打赏
  • 举报
回复
找本数据结构看一下,自己思考一下;不是挺简单的吗
还要有个互斥

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧