Thread t1 = new Thread(s, "producer1");
Thread t2 = new Thread(s, "consumer2");
t1.start();
// 如果你加这么一句,结果就会更大跌眼镜                            
t2.setPriority(Thread.MAX_PRIORITY);
t2.start();可以以把"线程"理解为程序内部的小"进程"
我们平时使用电脑上网的同时开BT下载,QQ聊天,看网页...
而这些都是同时进行的,不会出现下载的时候你只能等着什么也不能干,
聊天的时候不能下载,看网页的时候不能下载等等
进程和线程原理相近只不过有细微差别,
这些差别在学习操作系统原理的时候就会了解到

解决方案 »

  1.   

    我可以理解线程的并行执行,但是t1.start()后它停在那里,直至t2.start()然后notify()它。这时最重要,根据我的测试
    当t2notify后   
      count--;
       notifyAll();
       System.out.println("ac");//(1)
    在这里还是t2运行的。但t2还没有完成run()里面的代码,就被t1抢了而且比t2更早到达run()
    直到t1完成后,t2才又到达run().
    我就是不明白这里面到底是什么机制。
      

  2.   

    时间片的概念,每个线程都有机会获得一小段时间片来运行自己的指令,但是系统并不管你函数是否运行完成,到了时间片就会将你挂起,然后将时间片分配给别的线程或进程。并不是t1抢了t2的时间,而是刚好系统将t2挂起了,然后随机地找到了t1,运行了t1的指令。其实t1的run()函数也有可能是在好几个时间片内才完成所有的指令执行的,(如果汉书里面的指令比较多的话)只不过这期间系统没有将时间片分配给t2,这样就看起来好像t2被t1抢了CPU一样。
      

  3.   

    Thread t1 = new Thread(s, "producer1");
    Thread t2 = new Thread(s, "consumer2");
    t1.start();
    // 如果你加这么一句,结果就会更大跌眼镜                            
    t2.setPriority(Thread.MAX_PRIORITY);
    t2.start();
      

  4.   

    这个不是机制的问题
    而是你程序的问题
    你在两句 while 循环里面(try外面) 加上两句输出试试
    例:
    while (count == 0) {              
        System.out.println("acquire");
        ... ...
    }
    你就会发现问题了
      

  5.   

    非常感谢大家的解释,
    TO: registered(已注册)
    对于你所解释的时间片的概念,我还有点不明白。根据你所说的时间片是随机的话。那应该有机会。当t2 唤醒t1时
    count--;
       notifyAll();
       System.out.println("ac");//(1)
    系统也有机会这时的时间片分还是分给t2然后去执行run()里面的其它语句。
    但为什么我每次运行时都是t2唤醒t1后,总是t1先去执行run()里的其它内容。
    是不是是因为是t1先启动还是其它原因。
      

  6.   

    基本上理解了,
    http://www.pconline.com.cn/pcedu/empolder/gj/java/0403/341984_2.html
    再请问大家一个问题
          public int synMethod(int a1){
            synchronized(a1) {
              //一次只能有一个线程进入
            }
          }
    线程获得的是什么锁呀,是不是写错了。我在程序里这样用的话就会报错
      

  7.   

    系统总是想"平均分配"时间片
    之前"如果"进入了 t1 等待的话,
    那么时间片基本被 t2 都占用了,
    所以当 t2 唤醒 t1 的时候, CPU"很有可能"会被分配给 t1上面用"如果"是因为某些情况下,
    那个 while 循环根本不会进入(例如设置了优先级)synchronized(object)
    这个括号里面只能是 Object 的对象
    所以 int 那个用法是错误的
      

  8.   

    To:registered(已注册)
    public class Test {
    public static void main(String args[]) { Semaphore s = new Semaphore(1);
    Thread t1 = new Thread(s, "producer1");
    Thread t2 = new Thread(s, "consumer2");
    t1.start();
    t2.start();
    }
    }class Semaphore implements Runnable {
    private int count; int i = 0, j = 0; public Semaphore(int n) {
    this.count = n;
    } public synchronized void acquire() { System.out.println("j=" + j++);
    } public synchronized void release() {
    System.out.println("i=" + (i++));
    } public void run() {
    int o = 0;
    int q = 0;
    synchronized (this) {
    while (o < 1000) { if (Thread.currentThread().getName().substring(0, 8)
    .equalsIgnoreCase("consumer")) {
    System.out.println(q);
    } else if (Thread.currentThread().getName().substring(0, 8)
    .equalsIgnoreCase("producer")) {
    System.out.println("producer" + q++);
    }
    o++;
    } } }
    }
    还有二个疑问
    第一个就是对synchronized执行对象锁,第二个问题就是线程间的数据共享
    在上述的程序中
    Thread t1 = new Thread(s, "producer1");
    Thread t2 = new Thread(s, "consumer2");
    这二句是不是都让对象s去执行。也就是说只生成一个对象。
    这是因为synchronized (this){还有一个问题就是这里把this改为Semaphore.class效果是不是一样},执行的是对象锁,所以线程t2执行到synchronized (this)这里的时候它就只能等t1执行完成释放了这个对象,然后它才有机会执行。而运行的结果也如我预期。结果为
    producer0
    ~~~~~
    producer999
    0
    ~~
    0
    注意了当我把synchronized (this)移一下位置,如下
    while (o < 1000) {
    synchronized (this) {
    if (Thread.currentThread().getName().substring(0, 8)
    .equalsIgnoreCase("consumer")) {
    System.out.println(q);
    } else if (Thread.currentThread().getName().substring(0, 8)
    .equalsIgnoreCase("producer")) {
    System.out.println("producer" + q++);
    } }
    o++;
    }
    入在了while里面的时候,我想结果也应该同上,但是运行的结果也出乎了我的预料。不知道为什么。
    还有这二个问题就是线程间的数据共享问题。
    我这里System.out.println(q);不是应该打印t1执行后的q值吗,但是为什么运行时q=0;哈哈不好意思写了这么长。辛苦了各位
      

  9.   

    System.out.println(q);这个 q

    System.out.println("producer" + q++);这个 q
    完全是两个不同对象里的成员,谈不上共享数据
    synchronized (this)
    这个 this 是一对象,也就是说是 Semaphore.class 的一个实例(instance)
    推荐你用 Thinking In Java 或者 Core Java 里面的相关章节系统的学习一下
    不要东拼西凑,网上随便找文章看不如看"好"书
      

  10.   


    先谢谢registered(已注册)的指导了,明天就去买本Thinking In Java 来看下。哈哈哈
      

  11.   

    请问,
    public synchronized void acquire() {
       while (count == 0) {
         try {
           wait();
         } catch (InterruptedException e) {
           //keep trying
         }
       }
       count--;
       notifyAll();
       System.out.println("ac");//(1)
     }
    这儿的while换成if不能工作吗???为什么???