- public class TestThread implements Runnable{
- //int b=100;
- private TestMy tm=new TestMy();
- public void run(){
- tm.m1();
- }
- public void callm2(){
- tm.m2();
- }
- public void main(String []args){
- System.out.println("hello");
- TestThread th=new TestThread();
- Thread t1=new Thread(th);
- t1.start();
- th.callm2();
- }
- }
- class TestMy{
- int b=100;
- public synchronized void m1(){
- System.out.println("m1 method start");
- b=1000;
- try{
- Thread.sleep(5000);
- }
- catch(InterruptedException e){
- }
- System.out.println(b);
- System.out.println("m1 method over");
- }
- public void m2(){
- System.out.println("m2 method start");
- System.out.println(b);
- System.out.println("m1 method over");
- }
- }
总结:
祝线程和被创建的线程之间,一个是调用对象的synchronized方法
一个是调用同一个对象的非synchronized方法,这个时候二者之间并没有实现同步,估计应该和二者都没有加
同步关键字synchronized的效果一样