62,614
社区成员
发帖
与我相关
我的任务
分享
/**
* 要求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;
}
}
/**
* 要求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;
}
}
/**
* 要求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;
}
}