cainiaodebuguilu
2017-04-25 10:22
采纳率: 12.5%
浏览 1.0k
已采纳

java 多线程为什么锁没用?

public class ThreadsTest {

public static void main(String[] args) {
    Threads tsa=new Threads();
    Threads tss=new Threads();
    Threads tsd=new Threads();
    Threads tsf=new Threads();
    Threads tsg=new Threads();
    Thread ts1=new Thread(tsa, "线程1");
    Thread ts2=new Thread(tss, "线程2");
    Thread ts3=new Thread(tsd, "线程3");
    Thread ts4=new Thread(tsf, "线程4");
    Thread ts5=new Thread(tsg, "线程5");
    ts1.start();
    ts2.start();
    ts3.start();
    ts4.start();
    ts5.start();
}

}

package com.huadian.bean;

public class Threads implements Runnable{
private int str=5;

public Threads(int str) {
    super();
    this.str = str;
}

public Threads() {
    super();
}

@Override
public void run() {
    synchronized (Threads.class) {
        for(int i=0;i<str;i++){
            System.out.println(Thread.currentThread().getName()+"___"+i);
            if(i==3){
                try {
                    System.out.println(Thread.currentThread().getName()+"___"+"休息休息-----------------------------");
                    Thread.sleep(1000);
                //  Threads.class.notifyAll();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}

}

6条回答 默认 最新

相关推荐 更多相似问题