进程并发的常见问题

package org.example;



 //todo 死锁(Deadlock):多个线程相互等待对方持有的资源,导致所有线程无法继续执行。

public class DeadlockDemo {
    private static Object resource1 = new Object();
    private static Object resource2 = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (resource1) {
                System.out.println("Thread 1 acquired resource1");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resource2) {
                    System.out.println("Thread 1 acquired resource2");
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (resource2) {
                System.out.println("Thread 2 acquired resource2");
                synchronized (resource1) {
                    System.out.println("Thread 2 acquired resource1");
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}

package org.example;


//todo
// 在Java中,模拟并发执行可能涉及到多个常见问题,如竞态条件(Race Condition)、死锁(Deadlock)、饥饿(Starvation)等。下面我将分别介绍这些问题,并给出一个简单的Java示例来模拟这些问题:
// 竞态条件(Race Condition):多个线程同时访问共享资源,导致数据不一致或意外行为。

public class RaceConditionDemo {
    private static int counter = 0;

    public static void main(String[] args) {
        Runnable task = () -> {
            for (int i = 0; i < 10000; i++) {
                counter++; // 共享资源递增操作
            }
        };

        Thread thread1 = new Thread(task);
        Thread thread2 = new Thread(task);

        thread1.start();
        thread2.start();

        // 等待两个线程执行完毕
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Final counter value: " + counter);
    }
}

package org.example;

import java.util.concurrent.locks.ReentrantLock;

//todo 饥饿(Starvation):某些线程持续等待获得资源,但始终无法获得执行机会。
public class StarvationDemo {
    private static ReentrantLock lock = new ReentrantLock(true);
    public static void main(String[] args) {
        Runnable task = () -> {
            System.out.println(Thread.currentThread().getName() + " is trying to acquire lock");
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " acquired lock");
            try {
                Thread.sleep(1000); // 模拟任务执行
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        };
        // 创建多个线程来执行任务
        for (int i = 1; i <= 5; i++) {
            Thread thread = new Thread(task, "Thread-" + i);
            thread.start();
        }
    }
}
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超维Ai编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值