同步和非同步方法是否可以同时调用?

									## 同步和非同步方法是否可以同时调用?
package cn.qqjx.thread;

/*
 * @Auther  wangpeng
 * 同步和非同步方法是否可以同时调用?
 * @Date 2021/1/8
 */

public class MyThread {
    
    

    public synchronized void m1() {
    
    
        System.out.println(Thread.currentThread().getName() + " m1 start...");
        try {
    
    
            Thread.sleep(10000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " m1 end");
    }

    public void m2() {
    
    
        try {
    
    
            Thread.sleep(5000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " m2 ");
    }

    public static void main(String[] args) {
    
    
        MyThread t = new MyThread();

		/*new Thread(()->t.m1(), "t1").start();
		new Thread(()->t.m2(), "t2").start();*/

        new Thread(t::m1, "t1").start();
        new Thread(t::m2, "t2").start();

		/*
		//1.8之前的写法
		new Thread(new Runnable() {

			@Override
			public void run() {
				t.m1();
			}

		});
		*/

    }

}

t1 m1 start...
t2 m2 
t1 m1 end

猜你喜欢

转载自blog.csdn.net/m0_52936310/article/details/112343875