/**
 * 要求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;

}
}

解决方案 »

  1.   


    /**
     * 要求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;
    }
    }
      

  2.   

    在我写的这个程序中,为什么用synchronized关键字做该题不行呢?
      

  3.   

    用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;
    }
    }
      

  4.   

    这样呢
       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;
            }    }
      

  5.   

    在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;
    }
    }
      

  6.   

    我运行了一下出现了错误,如果一般是对零界资源的互斥访问用Semaphore好,还是snychronized好呢?
      

  7.   

    我表示,如果操作系统中信号量机制学的好的话,还是尽量用信号量吧,毕竟信号量能处理所有多线程问题
    另外不论什么语言,它们可能没有统一的类似synchronized这样的关键字,但是肯定有信号量机制