Java多线程

Java多线程,第1张

Java多线程

文章目录
      • ReentrantLock类
        • ReentrantLock类的使用
        • 使用Condition实现等待|通知
        • 生产者和消费者模式
        • 公平锁与非公平锁
      • 使用ReentrantReadWriteLock类
        • 读读共享

ReentrantLock类 ReentrantLock类的使用

package multiply.com.test;
public class Run {
    public static void main(String[] args) throws InterruptedException {
        MyService service = new MyService();
        ThreadA a = new ThreadA(service);
        ThreadAA aa = new ThreadAA(service);
        a.setName("A");
        aa.setName("AA");
        a.start();
        aa.start();
    }
}
package multiply.com.test;
public class ThreadA extends Thread {
    private MyService service;
    public ThreadA(MyService service) {
        this.service = service;
    }
    @Override
    public void run() {
        super.run();
        service.methodA();
    }
}
package multiply.com.test;
public class ThreadAA extends Thread {
    private MyService service;

    public ThreadAA(MyService service) {
        this.service = service;
    }
    @Override
    public void run() {
        service.methodA();
    }
}
package multiply.com.test;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyService {
    private Lock lock = new ReentrantLock();
    public void methodA() {
        try {
            lock.lock();
            System.out.println("method A begin Thread name = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("method A   end Thread name = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}

method A begin Thread name = A time = 1635413024078
method A end Thread name = A time = 1635413029079
method A begin Thread name = AA time = 1635413029080
method A end Thread name = AA time = 1635413034081

使用Condition实现等待|通知

package multiply.com.test;
public class Run {
    public static void main(String[] args) throws InterruptedException {
        MyService service = new MyService();
        ThreadA a = new ThreadA(service);
        ThreadB b = new ThreadB(service);
        a.setName("A");
        b.setName("B");
        a.start();
        b.start();
        Thread.sleep(3000);
        service.signalAll_A();
    }
}
package multiply.com.test;
public class ThreadA extends Thread {
    private MyService service;
    public ThreadA(MyService service) {
        this.service = service;
    }
    @Override
    public void run() {
        super.run();
        service.awaitA();
    }
}
package multiply.com.test;
public class ThreadB extends Thread {
    private MyService service;
    public ThreadB(MyService service) {
        this.service = service;
    }
    @Override
    public void run() {
        super.run();
        service.awaitB();
    }
}
package multiply.com.test;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyService {
    private Lock lock = new ReentrantLock();
    public Condition conditionA = lock.newCondition();
    public Condition conditionB = lock.newCondition();
    public void awaitA() {
        try {
            lock.lock();
            System.out.println("begin awaitA time = " + System.currentTimeMillis() + " thread name : " + Thread.currentThread().getName());
            conditionA.await();
            System.out.println("  end awaitA time = " + System.currentTimeMillis() + " thread name : " + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void awaitB() {
        try {
            lock.lock();
            System.out.println("begin awaitB time = " + System.currentTimeMillis() + " thread name : " + Thread.currentThread().getName());
            conditionB.await();
            System.out.println("  end awaitB time = " + System.currentTimeMillis() + " thread name : " + Thread.currentThread().getName());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void signalAll_A() {
        try {
            lock.lock();
            System.out.println("signalAll_A time = " + System.currentTimeMillis() + " thread name = " + Thread.currentThread().getName());
            conditionA.signalAll();
        }finally {
            lock.unlock();
        }
    }
    public void signalAll_B(){
        try {
            lock.lock();
            System.out.println("signalAll_B time = " + System.currentTimeMillis() + " thread name = " + Thread.currentThread().getName());
            conditionB.signalAll();
        }finally {
            lock.unlock();
        }
    }
}

begin awaitB time = 1635425117297 thread name : B
begin awaitA time = 1635425117298 thread name : A
signalAll_A time = 1635425120297 thread name = main
end awaitA time = 1635425120297 thread name : A

生产者消费者模式
package multiply.com.test;
public class Run {
    public static void main(String[] args) {
        MyService service = new MyService();
        ThreadA[] aThread = new ThreadA[10];
        ThreadB[] bThread = new ThreadB[10];
        for (int i = 0; i < 10; i++) {
            aThread[i] = new ThreadA(service);
            bThread[i] = new ThreadB(service);
            aThread[i].start();
            bThread[i].start();
        }
    }
}
package multiply.com.test;
public class ThreadA extends Thread {
    private MyService service;

    public ThreadA(MyService service) {
        this.service = service;
    }
    @Override
    public void run() {
        super.run();
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            service.set();
        }
    }
}
package multiply.com.test;
public class ThreadB extends Thread {
    private MyService service;

    public ThreadB(MyService service) {
        this.service = service;
    }
    @Override
    public void run() {
        super.run();
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            service.get();
        }
    }
}
package multiply.com.test;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyService {
    private Lock lock = new ReentrantLock();
    private Condition conditionB = lock.newCondition();
    private Condition conditionA = lock.newCondition();
    private boolean hasValue = false;
    public void set() {
        try {
            lock.lock();
            while (hasValue) {
                System.out.println("有可能1连续");
                conditionA.await();
            }
            System.out.println("打印1");
            hasValue = true;
            conditionB.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void get() {
        try {
            lock.lock();
            while (!hasValue) {
                System.out.println("有可能2连续");
                conditionB.await();
            }
            System.out.println("打印2");
            hasValue = false;
            conditionA.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}


公平锁与非公平锁















使用ReentrantReadWriteLock类

读读共享
package multiply.com.test;
public class Run {
    public static void main(String[] args) {
        Service service = new Service();
        ThreadA a = new ThreadA(service);
        a.setName("A");
        ThreadB b = new ThreadB(service);
        b.setName("B");
        a.start();
        b.start();
    }
}
package multiply.com.test;
public class ThreadA extends Thread {
    private Service service;
    public ThreadA(Service service) {
        super();
        this.service = service;
    }
    @Override
    public void run() {
        service.read();
    }
}
package multiply.com.test;
public class ThreadB extends Thread {
    private Service service;
    public ThreadB(Service service) {
        super();
        this.service = service;
    }
    @Override
    public void run() {
        service.read();
    }
}
package multiply.com.test;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Service {
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    public void read() {
        try {
            try {
                lock.readLock().lock();
                System.out.println("获得读锁" + Thread.currentThread().getName()
                        + " " + System.currentTimeMillis());
                Thread.sleep(10000);
            } finally {
                lock.readLock().unlock();
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}







欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4829197.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-10
下一篇 2022-11-10

发表评论

登录后才能评论

评论列表(0条)

保存