多线程,求帮忙该一下程序
/**
* 要求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;
}
}
* 要求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;
}
}
...全文
相关推荐
pthread多线程求pi,linux多线程pthread,C,C++
**错误处理**:在编写多线程程序时,必须考虑错误处理,如线程创建失败、同步操作出错等。`pthread`库提供了相应的错误码,程序员需要适当地检查并处理这些错误。
C++ 多线程求PI
在C++编程中,多线程技术是一种提升程序性能的有效方式,特别是在计算密集型任务中,如求解圆周率(PI)。这个项目名为“C++ 多线程求PI”,其目标是利用多线程并行计算来提高求解PI的效率。
用c#写的多线程求PI
标题中的“用C#写的多线程求PI”是指使用C#编程语言编写的一个程序,该程序通过多线程技术来计算圆周率π的值。
pi.rar_PI_linux 多线程_多线程_多线程求pi
这里的“pi.rar_PI_linux 多线程_多线程_多线程求pi”主题涉及到的是利用多线程技术在Linux操作系统上计算圆周率π的过程。
pthread多线程求pi,linux多线程pthread,C,C++源码.zip
在Linux中,pthread库是标准的多线程API,允许程序员创建和管理多个并发执行的线程。这些线程可以共享内存空间,协同完成复杂的任务,比如并行计算。多线程求π的经典算法之一是蒙特卡洛方法。


Java SE
