多线程,求帮忙该一下程序

成一粒 2011-04-25 08:18:16
/**
* 要求5个窗口同时开始售出100张票
* 并保存这一百张票中在那些窗口(及5个线程)中售出了那些票
* */
import java.util.Random;
import java.util.Vector;
import java.util.LinkedList;
public class test{
public static void main(String[] args){
Runnable r=new CreatThread(5,100);
Thread t1=new Thread(r,"thread1");
Thread t2=new Thread(r,"thread2");
Thread t3=new Thread(r,"thread3");
Thread t4=new Thread(r,"thread4");
Thread t5=new Thread(r,"thread5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class CreatThread implements Runnable{
private Object obj=1;
private int M;//存储总量
private int N;//窗口个数
private Vector<Integer> store;
private LinkedList<Integer> h1;
private LinkedList<Integer> h2;
private LinkedList<Integer> h3;
private LinkedList<Integer> h4;
private LinkedList<Integer> h5;

public CreatThread(int N,int M){
this.M=M;
this.N=N;
this.store=new Vector<Integer>();
this.store.setSize(N);//存储总量
for(int i=0;i<M;i++){
store.add(i);
}

this.h1=new LinkedList<Integer>();
this.h2=new LinkedList<Integer>();
this.h3=new LinkedList<Integer>();
this.h4=new LinkedList<Integer>();
this.h5=new LinkedList<Integer>();
}
public void run(){
synchronized(obj){
int i=0;
while(i<this.M){
Random ra=new Random();
int num=ra.nextInt(this.M);
if(store.contains(num)){
GetThreadArray(Thread.currentThread().getName()).add(num);
store.remove(store.indexOf(num));
}
}
}
}

public LinkedList<Integer> GetThreadArray(String name){
if(name.equals("thread1"))
return h1;
if(name.equals("thread2"))
return h2;
if(name.equals("thread3"))
return h3;
if(name.equals("thread4"))
return h4;
else
return h5;

}
}
...全文
196 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
周靖峰 2011-04-27
  • 打赏
  • 举报
回复
我表示,如果操作系统中信号量机制学的好的话,还是尽量用信号量吧,毕竟信号量能处理所有多线程问题
另外不论什么语言,它们可能没有统一的类似synchronized这样的关键字,但是肯定有信号量机制
周靖峰 2011-04-26
  • 打赏
  • 举报
回复
在while里面加一个延时就可以了,估计synchronized的对时间片的处理和Semaphore的处理不一样,加一个延时就能达到和Semaphore一样的效果了

/**
* 要求5个窗口同时开始售出100张票
* 并保存这一百张票中在那些窗口(及5个线程)中售出了那些票
* */
import java.util.Random;
import java.util.Vector;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;

public class Test
{
public static void main(String[] args)
{
Runnable r=new CreatThread(5,100);
Thread t1=new Thread(r,"thread1");
Thread t2=new Thread(r,"thread2");
Thread t3=new Thread(r,"thread3");
Thread t4=new Thread(r,"thread4");
Thread t5=new Thread(r,"thread5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}

class CreatThread implements Runnable
{
private Object obj=1;
private Object obj2 = 1;
int i=0;
private int M;//存储总量
private int N;//窗口个数
private Vector<Integer> store;
private LinkedList<Integer> h1;
private LinkedList<Integer> h2;
private LinkedList<Integer> h3;
private LinkedList<Integer> h4;
private LinkedList<Integer> h5;
private Semaphore mutex = new Semaphore(1);
private Semaphore mutex2 = new Semaphore(1);

public CreatThread(int N,int M)
{
this.M=M;
this.N=N;
this.store=new Vector<Integer>();
this.store.setSize(N);//存储总量
for(int i=0;i<M;i++)
{
store.add(i);
}
this.h1=new LinkedList<Integer>();
this.h2=new LinkedList<Integer>();
this.h3=new LinkedList<Integer>();
this.h4=new LinkedList<Integer>();
this.h5=new LinkedList<Integer>();
}

public void run()
{
while(i<this.M)
{
Random ra=new Random();
int num=ra.nextInt(this.M);
synchronized(obj) //互斥取数
{
if(store.contains(num))
{
GetThreadArray(Thread.currentThread().getName()).add(num);
store.remove(store.indexOf(num));
i++;
}
}
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
synchronized(obj2) //互斥打印
{
String name = Thread.currentThread().getName();
System.out.println(name);
for (int i : GetThreadArray(name))
System.out.println(i);
}
}

public LinkedList<Integer> GetThreadArray(String name)
{
if(name.equals("thread1"))
return h1;
if(name.equals("thread2"))
return h2;
if(name.equals("thread3"))
return h3;
if(name.equals("thread4"))
return h4;
else
return h5;
}
}

leisore 2011-04-26
  • 打赏
  • 举报
回复
这样呢
public CreatThread(int N, int M) {
this.M = M;
this.N = N;
this.store = new Vector<Integer>();
// this.store.setSize(N);//存储总量
for (int i = 0; i < M; i++) {
store.add(i);
}

this.h1 = new LinkedList<Integer>();
this.h2 = new LinkedList<Integer>();
this.h3 = new LinkedList<Integer>();
this.h4 = new LinkedList<Integer>();
this.h5 = new LinkedList<Integer>();
}

public void run() {
while (true) {
synchronized (obj) {
if (store.size() == 0) {
break;
}
Random ra = new Random();
int num = ra.nextInt(this.M);
if (store.contains(num)) {
GetThreadArray(Thread.currentThread().getName()).add(num);
store.remove(store.indexOf(num));
System.out.println("Thread:" + Thread.currentThread().getName() + " sale " + num);
}
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
}
}
}

public LinkedList<Integer> GetThreadArray(String name) {
if (name.equals("thread1")) {
return h1;
}
if (name.equals("thread2")) {
return h2;
}
if (name.equals("thread3")) {
return h3;
}
if (name.equals("thread4")) {
return h4;
} else {
return h5;
}

}
成一粒 2011-04-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 leisore 的回复:]
这样呢
public CreatThread(int N, int M) {
this.M = M;
this.N = N;
this.store = new Vector<Integer>();
// this.store.setSize(N);//存储总量
for (int i = 0; i < M; i++) {
store.add(i);
}……
[/Quote]
我运行了一下出现了错误,如果一般是对零界资源的互斥访问用Semaphore好,还是snychronized好呢?
成一粒 2011-04-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 nbda1121440 的回复:]
引用 2 楼 wuda236556254 的回复:
在我写的这个程序中,为什么用synchronized关键字做该题不行呢?

用synchronized也行,但是一定要加在临界区上面,上面那个程序用互斥量是因为我不太懂synchronized的用法,现在有点懂了,程序也可以写成这样

Java code

/**
* 要求5个窗口同时开始售出100张票
* 并保存这一百张票……
[/Quote]

你这样写用问题呀?我运行了N多次,总是会有一些线程没有输出结果,同时也没有卖出一张票
我想应该是边买票边存储票买完了后再用一个方法专门来输出!
周靖峰 2011-04-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuda236556254 的回复:]
在我写的这个程序中,为什么用synchronized关键字做该题不行呢?
[/Quote]
用synchronized也行,但是一定要加在临界区上面,上面那个程序用互斥量是因为我不太懂synchronized的用法,现在有点懂了,程序也可以写成这样

/**
* 要求5个窗口同时开始售出100张票
* 并保存这一百张票中在那些窗口(及5个线程)中售出了那些票
* */
import java.util.Random;
import java.util.Vector;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;

public class Test
{
public static void main(String[] args)
{
Runnable r=new CreatThread(5,100);
Thread t1=new Thread(r,"thread1");
Thread t2=new Thread(r,"thread2");
Thread t3=new Thread(r,"thread3");
Thread t4=new Thread(r,"thread4");
Thread t5=new Thread(r,"thread5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}

class CreatThread implements Runnable
{
private Object obj=1;
private Object obj2 = 1;
int i=0;
private int M;//存储总量
private int N;//窗口个数
private Vector<Integer> store;
private LinkedList<Integer> h1;
private LinkedList<Integer> h2;
private LinkedList<Integer> h3;
private LinkedList<Integer> h4;
private LinkedList<Integer> h5;
private Semaphore mutex = new Semaphore(1);
private Semaphore mutex2 = new Semaphore(1);

public CreatThread(int N,int M)
{
this.M=M;
this.N=N;
this.store=new Vector<Integer>();
this.store.setSize(N);//存储总量
for(int i=0;i<M;i++)
{
store.add(i);
}
this.h1=new LinkedList<Integer>();
this.h2=new LinkedList<Integer>();
this.h3=new LinkedList<Integer>();
this.h4=new LinkedList<Integer>();
this.h5=new LinkedList<Integer>();
}

public void run()
{
while(i<this.M)
{
Random ra=new Random();
int num=ra.nextInt(this.M);
synchronized(obj) //互斥取数
{
if(store.contains(num))
{
GetThreadArray(Thread.currentThread().getName()).add(num);
store.remove(store.indexOf(num));
i++;
}
}
}
synchronized(obj2) //互斥打印
{
String name = Thread.currentThread().getName();
System.out.println(name);
for (int i : GetThreadArray(name))
System.out.println(i);
}
}

public LinkedList<Integer> GetThreadArray(String name)
{
if(name.equals("thread1"))
return h1;
if(name.equals("thread2"))
return h2;
if(name.equals("thread3"))
return h3;
if(name.equals("thread4"))
return h4;
else
return h5;
}
}
成一粒 2011-04-25
  • 打赏
  • 举报
回复
在我写的这个程序中,为什么用synchronized关键字做该题不行呢?
周靖峰 2011-04-25
  • 打赏
  • 举报
回复

/**
* 要求5个窗口同时开始售出100张票
* 并保存这一百张票中在那些窗口(及5个线程)中售出了那些票
* */
import java.util.Random;
import java.util.Vector;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;

public class Test
{
public static void main(String[] args)
{
Runnable r=new CreatThread(5,100);
Thread t1=new Thread(r,"thread1");
Thread t2=new Thread(r,"thread2");
Thread t3=new Thread(r,"thread3");
Thread t4=new Thread(r,"thread4");
Thread t5=new Thread(r,"thread5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}

class CreatThread implements Runnable
{
private Object obj=1;
int i=0;
private int M;//存储总量
private int N;//窗口个数
private Vector<Integer> store;
private LinkedList<Integer> h1;
private LinkedList<Integer> h2;
private LinkedList<Integer> h3;
private LinkedList<Integer> h4;
private LinkedList<Integer> h5;
private Semaphore mutex = new Semaphore(1);
private Semaphore mutex2 = new Semaphore(1);

public CreatThread(int N,int M)
{
this.M=M;
this.N=N;
this.store=new Vector<Integer>();
this.store.setSize(N);//存储总量
for(int i=0;i<M;i++)
{
store.add(i);
}
this.h1=new LinkedList<Integer>();
this.h2=new LinkedList<Integer>();
this.h3=new LinkedList<Integer>();
this.h4=new LinkedList<Integer>();
this.h5=new LinkedList<Integer>();
}

public void run()
{
while(i<this.M)
{
Random ra=new Random();
int num=ra.nextInt(this.M);
try
{
mutex.acquire(); //mutex用于读数的互斥
if(store.contains(num))
{
GetThreadArray(Thread.currentThread().getName()).add(num);
store.remove(store.indexOf(num));
i++;
}
mutex.release();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
try
{
mutex2.acquire(); //mutex2用于实现打印的互斥
String name = Thread.currentThread().getName();
System.out.println(name);
for (int i : GetThreadArray(name))
System.out.println(i);
mutex2.release();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

public LinkedList<Integer> GetThreadArray(String name)
{
if(name.equals("thread1"))
return h1;
if(name.equals("thread2"))
return h2;
if(name.equals("thread3"))
return h3;
if(name.equals("thread4"))
return h4;
else
return h5;
}
}

62,614

社区成员

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

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