package me.luger.thread;public class SynchronizedTest { public static void main(String[] args) {
Synchronized1 s1 = new Synchronized1("s1");
Synchronized1 s2 = new Synchronized1("s2");
s1.start();
s2.start();
}
}class Synchronized1 extends Thread{

Synchronized1(String name){
super(name);
} public void run() {
m1();
}

synchronized void m1(){
for(int i = 0;i<1000;i++){
System.out.println(getName()+"------m1--------");
}
}

}
这个是我写的线程锁 我想测试的是“被锁定指的是 其他线程不能访问这个方法”但是这个好像看不出结果来  是不是我例子写的不对 哪位大神给一个能测试上面这句话的例子
对了还有一个问题 被锁定是指其他线程不能访问这个方法 还是不能访问这个类的所有带锁的方法

解决方案 »

  1.   

    因为你是两个不同的实例,所以相互之间不会互相影响的,请把:
        synchronized void m1(){
    修改为:
        static synchronized void m1(){
    就可以看到效果了。因为静态函数是类级别互斥访问的。
      

  2.   

    如果两个线程访问同一个方法,要看是访问静态方法还是非静态方法:--如果是静态方法,那么当方法声明为 synchronized 时,同一时间将只有一个线程执行这个方法;--如果是非静态方法,那要看这些线程是不是调用的同一个对象:------如果是同一个对象,那么当方法声明为 synchronized 时,同一时间将只有一个线程执行这个方法;------如果不是同一个对象,那么线程将同时运行。
      

  3.   

    这样试试:
    package me.luger.thread;public class SynchronizedTest {    public static void main(String[] args) {
            Synchronized1 s1 = new Synchronized1("s1");
            //Synchronized1 s2 = new Synchronized1("s2");
            new Thread(s1).start();
            new Thread(s1).start();
        }
    }class Synchronized1 extends Thread{
        
        Synchronized1(String name){
            super(name);
        }    public void run() {
            m1();
        }
        
        synchronized void m1(){
            for(int i = 0;i<10;i++){
                System.out.println(Thread.currentThread().getName()+"------m1--------");
            }
        }
        
    }
      

  4.   

    public class SynDemo
    {
    public static void main(String[] args)
    {
    Test test = new Test();

    new T1(test).start();
    new T2(test).start();
    }
    }class T1 extends Thread
    {
    private Test test;

    T1(Test test)
    {
    this.test = test;
    }

    @Override
    public void run()
    {
    for(int i = 0; i < 10; i ++)
    test.test();
    }
    }class T2 extends Thread
    {
    private Test test;

    T2(Test test)
    {
    this.test = test;
    }

    @Override
    public void run()
    {
    for(int i = 0; i < 10; i ++)
    test.test();
    }
    }class Test
    {
    synchronized public void test()
    {
    try
    {
    Thread.sleep((long)(Math.random() * 500));
    }
    catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + " hello");
    }
    }
      

  5.   

    public class Syn
    {
    public static void main(String[] args)
    {
    D d = new D();
    new MyThread(d).start();
    new MyThread(d).start();
    }
    }
    class MyThread extends Thread
    {
    private D d; public MyThread(D d)
    {
    this.d = d;
    } @Override
    public void run()
    {
    d.test();
    }
    }
    class D
    {
    synchronized public void test()
    {
    for(int i = 0 ; i < 10; i ++)
    {
    try
    {
    Thread.sleep((long)(Math.random() * 500));
    }
    catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + " hello");
    }
    }
    }