两个线程交替打印数字
使用Synchronized
注意wait()、notify()的使用
public class Comunication implements Runnable { private int num = 1; @Override public void run() { while (true) { synchronized (this) { this.notify(); if (num <= 100) { System.out.println(Thread.currentThread().getName() + "num:" + num); num++; } else { break; } try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
使用ReetLock、condition
condition.signal()、以及condition.await()
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; /** * @author SHshuo * @data 2022/2/24--18:23 */ public class Number implements Runnable{ private int num = 1; ReentrantLock lock = new ReentrantLock(); Condition condition = lock.newCondition(); @Override public void run() { while(true){ lock.lock(); try{ condition.signal(); if(num <= 100){ System.out.println(Thread.currentThread().getName() + "num:" + num); num++; }else { break; } condition.await(); }catch (InterruptedException e){ e.printStackTrace(); }finally { lock.unlock(); } } } }
共有部分
/** * @author SHshuo * @data 2022/2/24--18:11 */ public class Test { public static void main(String[] args) { Number c = new Number(); // Comunication c = new Comunication(); Thread t1 = new Thread(c, "线程A"); Thread t2 = new Thread(c, "线程B"); t1.start(); t2.start(); } }
创建10个线程、每个线程对一个共享变量进行加1、都加1w次
- 同花顺二面编程题
/** * @author SHshuo * @data 2022/9/26--16:34 * * 创建10个线程、每个线程对一个共享变量进行加1、都加1w次 */ public class hshuo { private static int nums = 1; private static Object lock = "lock"; public static void main(String[] args) { for(int i = 0; i < 10; i++) { new Thread(() -> { synchronized (lock) { for(int j = 0; j < 10000; j++) { System.out.println(Thread.currentThread().getName() + "nums:" + nums); nums++; } } }).start(); } } }
生产者和消费者模型
定义一个字符串进行加锁、等待、唤醒等操作;
package com.hshuo; /** * @author SHshuo * @data 2022/4/3--17:00 */ public class WaitTest { private static int count = 0; private static final int buffCount = 10; private static String lock = "lock"; class Producer implements Runnable { @Override public void run() { while(true) { try { // 每个1秒生产一个 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock) { while (count == buffCount) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } count++; System.out.println(Thread.currentThread().getName() + "-生产者生产,数量为:" + count); lock.notifyAll(); } } } } class Consumer implements Runnable { @Override public void run() { while(true) { try { // 消费者3秒消费一个 Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock) { while (count == 0) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } count--; System.out.println(Thread.currentThread().getName() + "-消费者消费,数量为:" + count); lock.notifyAll(); } } } } public static void main(String[] args) { WaitTest waitTest = new WaitTest(); // 10个消费者 for (int i = 0; i < 10; i++) { new Thread(waitTest.new Consumer()).start(); } new Thread(waitTest.new Producer()).start(); } }