Java多线程_对象及其变量的并发访问(2)

静态同步synchronized方法与synchronized(class)代码块

在这里插入图片描述

package multiply.com.test;
public class Run {
    
    
    public static void main(String[] args) {
    
    
        Service service = new Service();
        //同一个类,不同的对象
        ThreadA aThread = new ThreadA(service);
        ThreadB bThread = new ThreadB(service);
        ThreadC cThread = new ThreadC(service);
        aThread.setName("A");
        bThread.setName("B");
        cThread.setName("C");
        aThread.start();
        bThread.start();
        cThread.start();

    }
}
package multiply.com.test;
public class ThreadA extends Thread {
    
    
    private Service service;

    public ThreadA(Service service) {
    
    
        this.service = service;
    }
    @Override
    public void run() {
    
    
        super.run();
        service.printA();
    }
}
package multiply.com.test;
public class ThreadB  extends Thread {
    
    
    private Service service;
    public ThreadB(Service service) {
    
    
        this.service = service;
    }
    @Override
    public void run() {
    
    
        super.run();
        service.printB();
    }
}
package multiply.com.test;
public class ThreadC extends Thread {
    
    
    private Service service;
    public ThreadC(Service service) {
    
    
        this.service = service;
    }
    @Override
    public void run() {
    
    
        super.run();
        service.printC();
    }
}
package multiply.com.test;
public class Service {
    
    
    synchronized public static void printA() {
    
    
        try {
    
    
            System.out.println("thread name = " + Thread.currentThread().getName() + " in printA at " + System.currentTimeMillis());
            Thread.sleep(3000);
            System.out.println("thread name = " + Thread.currentThread().getName() + " out printA at " + System.currentTimeMillis());
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
    synchronized public static void printB() {
    
    
        System.out.println("thread name = " + Thread.currentThread().getName() + " in printB at " + System.currentTimeMillis());
        System.out.println("thread name = " + Thread.currentThread().getName() + " out printB at " + System.currentTimeMillis());
    }
    synchronized public void printC() {
    
    
        System.out.println("thread name = " + Thread.currentThread().getName() + " in printC at " + System.currentTimeMillis());
        System.out.println("thread name = " + Thread.currentThread().getName() + " out printC at " + System.currentTimeMillis());
    }
}

thread name = C in printC at 1634869837541
thread name = A in printA at 1634869837541
thread name = C out printC at 1634869837541
thread name = A out printA at 1634869840542
thread name = B in printB at 1634869840542
thread name = B out printB at 1634869840542
A和B是类锁,是并行的。C是对象锁是和A串行的。

数据类型String的常量池特性

package multiply.com.test;
public class Run {
    
    
    public static void main(String[] args) {
    
    
        ThreadA aThread = new ThreadA();
        ThreadB bThread = new ThreadB();
        aThread.setName("A");
        bThread.setName("B");
        aThread.start();
        bThread.start();
    }
}
package multiply.com.test;
public class ThreadA extends Thread {
    
    
    @Override
    public void run() {
    
    
        super.run();
        Service.print("AA");
    }
}
package multiply.com.test;
public class ThreadB extends Thread {
    
    
    @Override
    public void run() {
    
    
        super.run();
        Service.print("AA");
    }
}
package multiply.com.test;
public class Service {
    
    
    public static void print(String stringParam) {
    
    
        try {
    
    
            synchronized (stringParam) {
    
    
                while (true) {
    
    
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(1000);
                }
            }
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
}

B
B
B
B…
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

多线程的死锁

package multiply.com.test;
public class Run {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            DealThread t1 = new DealThread();
            t1.setFlag("a");
            Thread thread1 = new Thread(t1);
            thread1.start();
            Thread.sleep(100);
            t1.setFlag("b");
            Thread thread2 = new Thread(t1);
            thread2.start();
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
}
package multiply.com.test;
public class DealThread implements Runnable {
    
    
    public String username;
    public Object lock1 = new Object();
    public Object lock2 = new Object();
    public void setFlag(String username) {
    
    
        this.username = username;
    }
    @Override
    public void run() {
    
    
        if ("a".equals(username)) {
    
    
            synchronized (lock1) {
    
    
                try {
    
    
                    System.out.println("username = " + username);
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }

                synchronized (lock2) {
    
    
                    System.out.println("lock1 -> lock2");
                }
            }
        }
        if ("b".equals(username)) {
    
    
            synchronized (lock2) {
    
    
                try {
    
    
                    System.out.println("username = " + username);
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                synchronized (lock1) {
    
    
                    System.out.println("lock2 -> lock1");
                }
            }
        }

    }
}

内置类与静态内置类

在这里插入图片描述

package multiply.com.test;
public class Run {
    
    
    public static void main(String[] args) {
    
    
        final OutClass.Inner inner = new OutClass.Inner();
        Thread t1 = new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                inner.method1();
            }
        }, "A");
        Thread t2 = new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                inner.method2();
            }
        }, "B");
        t1.start();
        t2.start();
    }
}
package multiply.com.test;
public class OutClass {
    
    
    static class Inner {
    
    
        public void method1() {
    
    
            synchronized ("其他的锁") {
    
    
                for (int i = 0; i < 10; i++) {
    
    
                    System.out.println(Thread.currentThread().getName() + " i = " + i);
                    try {
    
    
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
            }
        }

        public synchronized void method2() {
    
    
            for (int i = 11; i < 20; i++) {
    
    
                System.out.println(Thread.currentThread().getName() + " i = " + i);
                try {
    
    
                    Thread.sleep(100);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

volatile关键字

在这里插入图片描述

解决同步死循环

package multiply.com.test;
public class Run {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        PrintString printStringService = new PrintString();
        new Thread(printStringService).start();
        System.out.println("I want to stop it ! stop thread = " + Thread.currentThread().getName());
        printStringService.setContinuePrint(false);
    }
}
package multiply.com.test;
public class PrintString implements Runnable {
    
    
    private boolean isContinuePrint = true;
    public boolean isContinuePrint() {
    
    
        return isContinuePrint;
    }
    public void setContinuePrint(boolean isContinuePrint) {
    
    
        this.isContinuePrint = isContinuePrint;
    }
    public void printStringMethod() {
    
    
        try {
    
    
            while (isContinuePrint == true) {
    
    
                System.out.println("run printStringMethod thread name = " + Thread.currentThread().getName());
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
    
    
        printStringMethod();
    }
}

I want to stop it ! stop thread = main
run printStringMethod thread name = Thread-0
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package multiply.com.test;
public class Run {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            RunThread thread = new RunThread();
            thread.start();
            Thread.sleep(1000);
            thread.setRunning(false);
            System.out.println("isRunning is set by false");
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
}
package multiply.com.test;
public class RunThread extends Thread {
    
    
    volatile private boolean isRunning = true;
    public boolean isRunning() {
    
    
        return isRunning;
    }
    public void setRunning(boolean isRunning) {
    
    
        this.isRunning = isRunning;
    }
    @Override
    public void run() {
    
    
        super.run();
        System.out.println("in run method ");
        while (isRunning == true) {
    
    

        }
        System.out.println("end run method");
    }
}

in run method
isRunning is set by false
end run method
在这里插入图片描述

synchronized代码块具有volatile数据同步功能

在这里插入图片描述

扫描二维码关注公众号,回复: 13229484 查看本文章
package multiply.com.test;
public class Run {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        Service service = new Service();
        ThreadA a = new ThreadA(service);
        a.start();
        Thread.sleep(1000);
        ThreadB b = new ThreadB(service);
        b.start();
        System.out.println("had been stopped !");
    }
}
package multiply.com.test;
public class ThreadA extends Thread{
    
    
    private Service service;

    public ThreadA(Service service) {
    
    
        this.service = service;
    }
    @Override
    public void run() {
    
    
        super.run();
        service.runMethod();
    }
}
package multiply.com.test;
public class ThreadB extends Thread {
    
    
    Service service;

    public ThreadB(Service service) {
    
    
        this.service = service;
    }
    @Override
    public void run() {
    
    
        super.run();
        service.stopMethod();
    }
}
package multiply.com.test;
public class Service {
    
    
    private boolean isContinueRun = true;
    synchronized public void runMethod() {
    
    
        String anyString = new String();
        while (true == isContinueRun) {
    
    
            synchronized (anyString) {
    
    
            }
        }
        System.out.println("stopped!");
    }

    public void stopMethod() {
    
    
        isContinueRun = false;
    }
}

had been stopped !
stopped!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42148307/article/details/120900492
今日推荐