常见代码问题

两个线程交替打印数字

使用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();
    }

}


hshuo的面试之路 文章被收录于专栏

作者目标是找到一份Java后端方向的工作 此专栏用来记录从Bilibili、书本、其他优质博客上面学习的内容 用于巩固、总结内容 主要包含Docker、Dubbo、Java基础、JUC、Maven、MySQL、Redis、SpringBoot、SpringCloud、数据结构、杂文、算法、计算机网络、操作系统、设计模式等相关内容

全部评论

相关推荐

不愿透露姓名的神秘牛友
2024-12-30 18:02
程序员牛肉:1.可以标记一下自己的学校是985,有一些hr可能没想到你这个院校是985的。 2.简历所呈现出来的能力还是有点差的,苍穹外卖+黑马点评。这在java技术域里面也就是刚学三四个月的样子,大厂现在招人少,小厂又更加希望你能直接过来干活。就你简历上呈现出来的能力,确实是有点难找,肉眼可见的不懂技术。 第一个项目中:简单的使用redis也算是亮点嘛?使用jwt,threadlocal也算是亮点?你不就是调了几个包嘛?Nginx作为服务器也能写出来,这不是前端的活嘛? 第二个项目中:分布式锁+mq消息队列+Lua队列。真没啥好问的。属于面试官看一眼就阳痿的简历,没有任何想提问的欲望。 我给你建议是好好的挖一挖这个项目吧,其实苍穹外卖和黑马点评这两个项目很不错了,只不过是太烂大街了导致面试官没啥问的兴趣,所以不太推荐写简历上。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务