13个就餐问题,拿刀拿叉问题,大家碰到过没,不知道谁能用JAVA来描述。 听说这还和多线程有关 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 买本书吧,兄弟,我被你的发贴精神所折服。有那位兄弟打字快把thinking java4中 哲学家相关的2页讲解 搞到论坛上来。 线程吧考虑进程那是dotnet的事情 顺便问一下:sure2003和lz是一个人吗?感觉学习的进度一样,感兴趣的问题也一样。 import java.util.Random;public class DiningPhils{ public static void main(String[] args) { int n = 10; if( n < 1) { System.err.println( "DiningPils <# of philosophers>" ); System.exit(-1); } DiningPhils self = new DiningPhils(); self.init(n); } public int getCount() { return n; } public void setChopstick( int i, boolean v) { chops[ i ] = v; } public boolean getChopstick( int i ) { return chops[i]; } private void init( final int N) { r = new Random(); n = ( N < 0 || N > maxPhils ) ? maxPhils : N; chops = new boolean[n]; phils = new Philosopher[n]; initPhils(); dumpStatus(); } private void initPhils() { for( int i = 0; i< n; i++ ) { phils[i] = new Philosopher( this, i ); phils[i].setTimeSlice( generateTimeSlice()); phils[i].setPriority( Thread.NORM_PRIORITY - 1); /**哲学家进程降低一级,使所有哲学家进程 *全部初始化完毕前不会有哲学家进程抢占主线程*/ } while( moreToStart() ) { int i = Math.abs( r.nextInt()) % n; if( !phils[i].isAlive()) { System.out.println( " ### Philosopher " + String.valueOf( i ) + " started."); phils[i].start(); } } System.out.println( "\nPhilosophers Chopsticks" + "\n(1 = eating, 0 = thinking) (1 = taken, 0 = free)"); } public int generateTimeSlice() { int ts = Math.abs(r.nextInt()) % (maxEat + 1); if( ts == 0 ) ts = minEat; return ts; } public void dumpStatus() { for( int i = 0; i < n; i++) System.out.print(phils[i].getEat() ? 1 : 0); for( int i = n; i < maxPhils + 4; i++ ) System.out.print(" "); for( int i = 0; i < n; i++) System.out.print(chops[i]? 1:0); System.out.println(); } private boolean moreToStart() { for( int i = 0; i < phils.length; i++ ) { if( !phils[i].isAlive()) return true; } return false; } private int n; private Philosopher[] phils; private boolean[] chops; private Random r; private static final int maxPhils = 24; //最多哲学家数 private static final int maxEat = 4; //最多进餐时间 private static final int minEat = 1; //最少进餐时间}class Philosopher extends Thread{ public Philosopher( DiningPhils HOST , int i ) { host = HOST; index = i; } public void setTimeSlice( int TS ) { ts = TS; } public void setLeftChopstick( boolean flag ) { host.setChopstick(index, flag); } public void setRightChopstick( boolean flag ) { host.setChopstick((index + 1)% host.getCount() , flag); } private void releaseChopsticks() { setLeftChopstick(false); setRightChopstick(false); } public boolean chopsticksFree() { return !host.getChopstick(index) && !host.getChopstick((index+1)%host.getCount()); } public void run() { while(true) { grabChopsticks(); eat(); think(); } } private synchronized void grabChopsticks() /**临界区函数,确保哲学家在没有筷子或筷子不够时思考,满足条件后才就餐*/ { while( !chopsticksFree()) { try { wait(); } catch( InterruptedException e){} } takeChopsticks(); notifyAll(); } private void takeChopsticks() { setLeftChopstick( true ); setRightChopstick( true ); setEat(true); host.dumpStatus(); } private void eat() { pause(); setEat( false ); releaseChopsticks(); } private void think() { pause(); } private void pause() { setTimeSlice( host.generateTimeSlice()); try { sleep(ts*1000); } catch( InterruptedException e){} } private void setEat(boolean v) { isEating = v; } public boolean getEat() { return isEating; } private DiningPhils host; private boolean isEating; private int index; private int ts;}btw:这代码不是我写的。错了不要怪我。自己写的找不到了可能丢了吧,毕竟也是2年前写的了 请问是什么问题呢? 高分求助图片等比缩放算法? JTable cell编辑颜色、文本 java怎么维护传入的变量 util的Date咋插入到sql的setDate中? 反射对性能影响大不 一个转换图像格式的程序,出错求救 [求助]连接access数据库的问题 在Applet中打开的Dialog如何与正在运行的Applet通讯 请问如何设置 classPath的多级可执行目录 ? 学习C#最好用什么编程环境? 再论main线程与子线程交互问题. [代码]
有那位兄弟打字快把thinking java4中 哲学家相关的2页讲解 搞到论坛上来。
考虑进程那是dotnet的事情
sure2003和lz是一个人吗?
感觉学习的进度一样,感兴趣的问题也一样。
public class DiningPhils
{
public static void main(String[] args)
{
int n = 10;
if( n < 1)
{
System.err.println( "DiningPils <# of philosophers>" );
System.exit(-1);
}
DiningPhils self = new DiningPhils();
self.init(n);
}
public int getCount()
{
return n;
}
public void setChopstick( int i, boolean v)
{
chops[ i ] = v;
}
public boolean getChopstick( int i )
{
return chops[i];
}
private void init( final int N)
{
r = new Random();
n = ( N < 0 || N > maxPhils ) ? maxPhils : N;
chops = new boolean[n];
phils = new Philosopher[n];
initPhils();
dumpStatus();
}
private void initPhils()
{
for( int i = 0; i< n; i++ )
{
phils[i] = new Philosopher( this, i );
phils[i].setTimeSlice( generateTimeSlice());
phils[i].setPriority( Thread.NORM_PRIORITY - 1);
/**哲学家进程降低一级,使所有哲学家进程
*全部初始化完毕前不会有哲学家进程抢占主线程*/
}
while( moreToStart() )
{
int i = Math.abs( r.nextInt()) % n;
if( !phils[i].isAlive())
{
System.out.println( " ### Philosopher " +
String.valueOf( i ) + " started.");
phils[i].start();
}
}
System.out.println( "\nPhilosophers Chopsticks"
+ "\n(1 = eating, 0 = thinking) (1 = taken, 0 = free)");
}
public int generateTimeSlice()
{
int ts = Math.abs(r.nextInt()) % (maxEat + 1);
if( ts == 0 )
ts = minEat;
return ts;
}
public void dumpStatus()
{
for( int i = 0; i < n; i++)
System.out.print(phils[i].getEat() ? 1 : 0);
for( int i = n; i < maxPhils + 4; i++ )
System.out.print(" ");
for( int i = 0; i < n; i++)
System.out.print(chops[i]? 1:0);
System.out.println();
}
private boolean moreToStart()
{
for( int i = 0; i < phils.length; i++ )
{
if( !phils[i].isAlive())
return true;
}
return false;
}
private int n;
private Philosopher[] phils;
private boolean[] chops;
private Random r;
private static final int maxPhils = 24; //最多哲学家数
private static final int maxEat = 4; //最多进餐时间
private static final int minEat = 1; //最少进餐时间
}
class Philosopher extends Thread
{
public Philosopher( DiningPhils HOST , int i )
{
host = HOST;
index = i;
}
public void setTimeSlice( int TS )
{
ts = TS;
}
public void setLeftChopstick( boolean flag )
{
host.setChopstick(index, flag);
}
public void setRightChopstick( boolean flag )
{
host.setChopstick((index + 1)% host.getCount() , flag);
}
private void releaseChopsticks()
{
setLeftChopstick(false);
setRightChopstick(false);
}
public boolean chopsticksFree()
{
return !host.getChopstick(index) &&
!host.getChopstick((index+1)%host.getCount());
}
public void run()
{
while(true)
{
grabChopsticks();
eat();
think();
}
}
private synchronized void grabChopsticks() /**临界区函数,确保哲学家在没有筷子或筷子不够时思考,满足条件后才就餐*/
{
while( !chopsticksFree())
{
try
{
wait();
}
catch( InterruptedException e){}
}
takeChopsticks();
notifyAll();
}
private void takeChopsticks()
{
setLeftChopstick( true );
setRightChopstick( true );
setEat(true);
host.dumpStatus();
}
private void eat()
{
pause();
setEat( false );
releaseChopsticks();
}
private void think()
{
pause();
}
private void pause()
{
setTimeSlice( host.generateTimeSlice());
try
{
sleep(ts*1000);
}
catch( InterruptedException e){}
}
private void setEat(boolean v)
{
isEating = v;
}
public boolean getEat()
{
return isEating;
}
private DiningPhils host;
private boolean isEating;
private int index;
private int ts;
}btw:这代码不是我写的。错了不要怪我。
自己写的找不到了可能丢了吧,毕竟也是2年前写的了