`
uule
  • 浏览: 6349916 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

自己的测试类

 
阅读更多

一、1

package test;

/**
* 消费者
*/
public class Consumer extends Thread{

	private int neednum;
	private Godown godown;
	
	Consumer(){
		
	}
	Consumer(int neednum, Godown godown){
		this.neednum = neednum;
		this.godown = godown;
	}
	public void run(){
		 //消费指定数量的产品
		godown.consume(neednum);
	}
}

 

2、

package test;
/**
 * 仓库 
 */
public class Godown {

	public static final int max_size = 100;  //最大库存量
	public int curnum;   //当前库存量
	
	Godown(){
		
	}
	Godown(int curnum){
		this.curnum = curnum;
	}
	
	 /**
     * 生产指定数量的产品 
     * @param neednum
     */
    public synchronized void produce(int neednum) {
            //测试是否需要生产
            while (neednum + curnum > max_size) {
                    System.out.println("要生产的产品数量" + neednum + "超过剩余库存量" + (max_size - curnum) + ",暂时不能执行生产任务!");
                    try {
                            //当前的生产线程等待
                            wait();
                    } catch (InterruptedException e) {
                            e.printStackTrace();
                    }
            }
            //满足生产条件,则进行生产,这里简单的更改当前库存量
            curnum += neednum;
            System.out.println("已经生产了" + neednum + "个产品,现仓储量为" + curnum);
            //唤醒在此对象监视器上等待的所有线程
            notifyAll();
    }

    /**
     * 消费指定数量的产品 
     * @param neednum
     */
    public synchronized void consume(int neednum) {
            //测试是否可消费
            while (curnum < neednum) {
                    try {
                            wait();
                    } catch (InterruptedException e) {
                            e.printStackTrace();
                    }
            }
            //满足消费条件,则进行消费,这里简单的更改当前库存量
            curnum -= neednum;
            System.out.println("已经消费了" + neednum + "个产品,现仓储量为" + curnum);
            //唤醒在此对象监视器上等待的所有线程
            notifyAll();
    } 
}

 3、

package test;

public class JoinTest{
	
	public static void main(String[] args) {
        Thread t = new Thread(new RunnableImpl());
        t.start();
        try {
            t.join(1000);
            System.out.println("joinFinish");
        } catch (InterruptedException e) {
            e.printStackTrace();     
        }
    }
	/**
	 *  Waits at most <code>millis</code> milliseconds for this thread to  
     * die. A timeout of <code>0</code> means to wait forever.	
	 */
	/*//此处A timeout of 0 means to wait forever 字面意思是永远等待,其实是等到t结束后。
	public final synchronized void join(long millis)    throws InterruptedException {
		long base = System.currentTimeMillis();
		long now = 0;

		if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
		}
		
		if (millis == 0) {
			while (isAlive()) {
				wait(0);
			}
		} else {
			while (isAlive()) {
				long delay = millis - now;
				if (delay <= 0) {
					break;
				}
				wait(delay);
				now = System.currentTimeMillis() - base;
			}
		}
    }*/
}

 4、

package test;

public class JoinThread extends Thread
{
    public static volatile int n = 0;

    public void run()
    {
        for (int i = 0; i < 10; i++, n++)
            try
            {
                sleep(3);  // 为了使运行结果更随机,延迟3毫秒
            }
            catch (Exception e)
            {
            }                                      
    }
    public static void main(String[] args) throws Exception
    {
        Thread threads[] = new Thread[50];
        for (int i = 0; i < threads.length; i++)  // 建立100个线程
            threads[i] = new JoinThread();
        for (int i = 0; i < threads.length; i++)   // 运行刚才建立的100个线程
            threads[i].start();
        if (args.length > 0)  
            for (int i = 0; i < threads.length; i++)   // 100个线程都执行完后继续
            	
                threads[i].join();
        System.out.println("n=" + JoinThread.n);
    }
}

 5、

package test;
/**
 * 生产者
 */
public class Producer extends Thread{

	private int neednum;  //生产产品的数量
	private Godown godown; //仓库
	
	Producer(){
		
	}
	Producer(int neednum,Godown godown){
		this.neednum = neednum;
		this.godown = godown;
	}
	
	public void run() {
		  //生产指定数量的产品
		godown.produce(neednum);
	}
}

 6、

package test;

class RunnableImpl implements Runnable {

    public void run() {
        try {
            System.out.println("Begin sleep");
            Thread.sleep(2000); //原来为1000
           System.out.println("End sleep");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

 7、

package test;

public class ThreadTest {

	public static void main(String[] args) {
		 Godown godown = new Godown(30);
         Consumer c1 = new Consumer(50, godown);
         Consumer c2 = new Consumer(20, godown);
         Consumer c3 = new Consumer(30, godown);
         Producer p1 = new Producer(10, godown);
         Producer p2 = new Producer(10, godown);
         Producer p3 = new Producer(10, godown);
         Producer p4 = new Producer(10, godown);
         Producer p5 = new Producer(10, godown);
         Producer p6 = new Producer(10, godown);
         Producer p7 = new Producer(80, godown);
         Producer p8 = new Producer(10, godown);

         c1.start();  //wait
         c2.start();  
         c3.start();  //wait  
         p1.start();
         p2.start();  //p2执行完后,仓储量为30,唤醒等待线程时,c3在等待且消费数量满足要求,故又执行c3
                      //已经生产了10个产品,现仓储量为30
         			  //已经消费了30个产品,现仓储量为0
         p3.start();
         p4.start();
         p5.start();
         p6.start();
         p7.start(); //wait
          			 //要生产的产品数量80超过剩余库存量60,暂时不能执行生产任务!
         p8.start();
	}
}

 

 

 

 

 

  • test.rar (5.6 KB)
  • 描述: 一、二的测试类文件
  • 下载次数: 2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics