今天,我决定尝试解决餐饮哲学家的问题。所以我写了下面的代码。但我认为这是不正确的,所以如果有人告诉我是怎么回事,我会很高兴的。我使用叉作为锁(我只读取它们,因为我不把对它们的访问放在同步块中),我有扩展线程的类,它保留了它的两个锁。
import java.util.Random;
public class EatingPhilosophersProblem {
private final static Random RANDOM = new Random();
/**
*
* @author Damyan Class represents eating of every phil
我一直在维基百科上寻找餐饮哲学家问题的解决方案。
我知道它是如何工作的,以及打破循环结构是如何防止死锁的,但是解决方案如何防止饥饿呢?难道一个或几个线程不能在少数线程无法取得进展时继续运行吗?
如果不是,是什么阻止了这种情况的发生?
实现:
public class DinningphilMain {
public static void main(String[] args) throws InterruptedException {
int numPhil = 3;
Philosopher[] phil = new Philosopher
我需要检查我的解决餐饮哲学家问题的算法,如果它保证了以下所有内容都满意或不满意:
不可能陷入僵局。
没有饿死的可能。
我正在用筷子上的来解决这个问题。
这是我的代码(算法):
while(true)
{
// He is Hungry
pickup_chopsticks(i);
// He is Eating...
drop_chopsticks(i);
// He is thinking
}
// ...
void pickup_chopsticks(int i)
{
if(i % 2 == 0) /* Even num
我试图在html中创建一个选项下拉列表,使用JQuery从数组中添加选项列表。
除了开始和结束标记之间的文本之外,所有东西似乎都正常工作。我是不是太累了,漏掉了一个简单的错误或者做错了什么?!?
JS和JQuery代码是:
var displayMenuSelections = function(){
var menuSelection = menu[0].prices[0];
var menuItems = Object.keys(menuSelection);
menuItems.forEach(menuFunction);
}
function menuFunction(i
这是个家庭作业问题,我弄错了。我真的很想了解这个问题的解决办法。原始代码状态:
The solution presented in Fig. 2-46 is deadlock-free and allows the maximum parallelism for an arbitrary number of philosophers. It uses an array, state, to keep track of whether a philosopher is eating, thinking, or hungry (trying to acquire forks). A philoso
我解决这个难题的逻辑就是让左右筷子自由,这样哲学家才能抓取和吃东西。我不写条件来打破时间循环,因为我假设那些哲学家吃东西,思考24/7/。
我的解决方案死锁现在没有了吗?请随时给我建议。我对多线程很陌生。
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
static int philosopher = 5;
static philosopher philosophers[] = new philosopher[p
我正在试图解决餐饮哲学家问题 in CSharp。我没有用信号灯,看上去很管用。我想知道1)我的代码死锁是否安全,2)我是否可以得到一些关于我的方法的反馈,而不是使用信号量。谢谢
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public enum State
{
Eating, Thinking,
}
internal class Program
{
private static
我正在尝试禁用我在@media中的CSS中使用的动画,这样它们就不会在移动设备上使用。HTML/CSS/JS在代码片段中。现在,通过将.open规则应用于.half-width-text,我成功地禁用了div的文本转换。
但是我如何禁用第一个列表的关键帧动画呢?
$(document).on('click', 'a[href^="#"]', function (event) {
//prevent direct jump to the linked element
event.preventDefault();
$(
我是react的新手,我正在构建这个游戏,它有随机出现在洞中的物体。如果你点击一个好的对象,它会增加分数,如果你点击一个不好的对象,它会失去分数。 我已经做了一个对象随机出现,在点击它将添加分数加上不会出现在相同的位置。但是当我尝试添加second时,它总是弹出在相同的位置,score停止工作。下面是一些代码 clearFishes(){
for(let value in this.state){
if (!isNaN(value)){
this.setState({
[value]: 'translate(0, 110%)'
});
我编写了这个程序来解决这个问题,通过仲裁解决算法,提到了这里,来解决这个问题。它指出,每个哲学家都应该请求Waiter的许可才能得到Fork。Philosopher吃东西要吃到两把叉子。我已经实现了逻辑,他要么握住两个叉子,要么放下,如果只有一个叉子。
我需要您对此代码进行审查,以了解有关编码实践、改进、可能的错误或替代解决方案的任何建议。
DiningAlgorithm
package com.study.fundamentals.util;
/**
* This is starting point for the dining algorithm
*/
我在为一位哲学家创造一条线索。以下是伪代码:
while(true)
think
get left chopstick
get right chopstick
eat
putdown left chopstick
putdown right chopstick
凭直觉,我不认为解除筷子的顺序会有什么关系。所以先放下左边的筷子,我能先放右筷子吗?这会导致什么错误吗?我怀疑会有什么结果。如果这会导致僵局,怎么做?如果没有,但首先推荐左,那为什么?
谢谢!
我刚刚完成了对餐饮哲学家问题的解决方案,但是我对我的代码没有信心,因为我仍然是并发世界的新手。如果你能给我一些反馈意见,我将不胜感激。
这是我的主修课:
public class DiningPhilosophersTable {
//An array holding all the chopsticks
private final Chopstick[] chopsticks = new Chopstick[5];
/*Constructor for the main class
* Creates all the chopsticks
* Cr