Sei sulla pagina 1di 2

import java.lang.*; import java.rmi.

*;
import java.util.*; import java.rmi.server.*;
import java.io.*; import javax.naming.*;
import java.awt.*; IOException throws RemoteException
import java.awt.event.*; ClassNotFoundException
import javax.swing.*; extends UnicastRemote!ject
import java.net.*; extends Remote
synchronized"oggetto# implements seriali$a!le % Runna!le
s&nchorni$ed pu!lic' noti(&)ll"#; % new *hread"o!j#.start"#;
extends Observable % noti(&!servers"#;
implements !server%ogg.add!server"this#;%update"!serva!le o!+ !ject o#
JFrame ,(; ,( - new Frame".titolo/#;
,(.set0e(aultCloseperation"1Frame.E23*4N4C56E#;
,(.setContent7ane"main7anel#;
,(.pac8"#; ,(.set9isi!le"true#;
ogg.addActionListener"new )ction5istener"#:
pu!lic void action7er(ormed")ctionEvent e#
: '.. ; ;#;
class altroListener implements )ction5istener :
pu!lic void action7er(ormed")ctionEvent e# :'; ;
ServerSocket server6; 6oc8et se; !ject ogg;
!ject3nput6tream in; !jectutput6tream out;
tr&% server6 - new 6erver6oc8et"port#; %3
tr&% se - server6.accept"#;
in - new !ject3nput6tream"se.get3nput6tream"##;
out - new !jectutput6tream"se.getutput6tream"##;
tr&% ogg - "!ject#in.read!ject"#; %Cl
out.write!ject"ogg#; out.(lush"#; %3
(in%tr&% in.close"#; out.close"#; se.close"#; server6.close"#; %3
class ServerImpl"# extends UnicastRemote!ject 3mplements 6erver
: pu!lic metodo"# throws RemoteException:..; ..;
main: tr&% 6erver3mpl serv - new 6erver3mpl"#;
Context namingContext - new 3nitialContext"#;
namingContext.!ind".rmi<server/+ serv#; %"Exception e#
pu!lic interface Server extends Remote:metodo"#throws RemExc;;
pu!lic inter(ace oggNonRem : metodo"..#;;
class oggNonRem3mpl implements oggNonRem+ 6eriali$a!le:';
class Client"# : pu!lic static void main"6tring=> args#:
tr&% Context namingContext - new 3nitialContext"#;
client oggettoClient - "client#namingContext.loo8up".rmi<server/#;
class ChopStick:
!oolean availa!le;
Chop6tic8"#:availa!le - true;;
pu!lic s&nchroni$ed void takep"#:
while "availa!le -- (alse#:
tr&:wait"#;; catch "3nterruptedException e#:; ;
availa!le - (alse; noti(&)ll"#; ;
pu!lic s&nchroni$ed void ptdo!n"#:
availa!le - true; noti(&)ll"#; ; ;
class "hilosopher extends *hread:
Chop6tic8 right; Chop6tic8 le(t; int philo4num;
7hilosopher"int num+ Chop6tic8 chop?+ Chop6tic8 chop@#:
philo4num - num; le(t - chop?; right - chop@;;
pu!lic void eat"#:
i("philo4numA@ --B#:
le(t.ta8eup"#; right.ta8eup"#; ;
else:
right.ta8eup"#; le(t.ta8eup"#; ; ;
pu!lic void think"#:
le(t.putdown"#; right.putdown"#; ;
pu!lic void rn"#:
while"true#:
eat"#; thin8"#; ; ; ;
class #inin$:
static Chop6tic8=> chopstic8s - new Chop6tic8=C>;
static 7hilosopher=> philos - new 7hilosopher=C>;
pu!lic static void main"6tring args=>#:
(or"int count - B; count D- E; countFF# :
chopstic8s=count> - new Chop6tic8"#; ;
(or"int count - B; count D- E; countFF#:
philos=count> - new 7hilosopher"count+
chopstic8s=count>+ chopstic8s="countF?#AC>#; ;
(or"int count - B; count D- E; countFF#
philos=count>.start"#; ; ;

Potrebbero piacerti anche