View Full Version : [JAVA] Processi e thread
Ed_Bunker
13-08-2005, 04:00
Scusatemi per l'impertinente ignoranza...
se ho una classe costuita da un singolo metodo main nel quale prima effettuo alcune stampe e poi creo ed eseguo due thread differenti cosa succede al processo main ?
Non dovrebbe continuare regolarmente la sua esecuzione e terminare (Le istruzioni in cui i thread vengono lanciati rappresentano le ultime due istruzioni nel metodo main) ?
Nel mio caso accade questo:
il processo main si blocca una volta invocato il metodo .start() per mandare in esecuzione il primo thread (Il quale, correttamente, va a bloccarsi in attesa di ricevere pacchetti da un multicast socket).
Il secondo thread, invece, non viene ne' creato ne' lanciato perche' il processo main si blocca prima.
Dove ho sbagliato ?!
P.S.: scusatemi ancora... :(
Il main dovrebe terminare. Facci vedere il codice.
Ed_Bunker
13-08-2005, 12:04
ackage taxi;
import java.net.*;
import java.io.*;
public class Taxi
{
public static final int A = 20;//Ride di tipo A
public static final int B = 30;//Ride di tipo B
public static final int C = 40;//Ride di tipo C
public static final String multicastAddress = "224.0.0.1";
public static final int taxiMulticastPort = 8888;
public static final int taxiuserMulticastPort = 9999;
public static final int taxyTCP_Port = 5555;
public static final int userTCP_Port = 6666;
public static final int MAXDIM = 2048;
public static final int MAXRESERVATIONS=5;
public static final int MAXRESMUL = 10000;
public static final int MAXTIME = 30;
public static final int MAXNOTIFY = 5;
public static void main (String[] args)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String myName=null;
//Se il parametro e' gia' stato passato da linea di comando
if (args.length == 1)
myName = args[0];
else
//Interazione attraverso lo standard input
{
try
{
System.out.println("Inserisci nome del taxi:");
myName = reader.readLine();
}
catch(IOException e)
{
System.out.println("Errore durante la lettura dallo stdin.");
System.exit(-1);
}
}
InetAddress myInetAdd = null;
try
{
myInetAdd = InetAddress.getByName("localhost");
}
catch (IOException e)
{
System.out.println("Impossibile creare l'inet address del localhost.");
System.exit(-1);
}
//Creo il mio profile
TaxiProfileImpl myProfile = new TaxiProfileImpl(myName, myInetAdd.getHostName(), taxyTCP_Port);
//Monitor
TaxiMon tm = new TaxiMon(myProfile);
//Creo il server socket
ServerSocket ss = null;
try
{
ss = new ServerSocket(taxyTCP_Port);
}
catch (IOException e)
{
System.out.println("Taxi " + myName + "> errore nella creazione del server socket.");
System.exit(-1);
}
System.out.println("Taxi " + myName + "> attivo.");
//Creazione degli handler
ReservationHandler rh = new ReservationHandler(ss, tm);
rh.run();
//QUI NON ARRIVO !!!
RequestHandler t = new RequestHandler(tm);
t.start();
System.out.println("Taxi " + myName + "> gli handler sono stati correttamente lanciati.");
}
}
ReservationHandler e RequestHandler sono costituiti soltanto dal relativo costruttore a dal metodo run().
Il primo viene lanciato ed esegue cio' che ci si attende mentre il process main, a quel punto, ne' costruisce ne' esegue il secondo thread. :confused:
franksisca
13-08-2005, 13:18
non devi fare rh.run(), ma rh.start().
I thread si avviano cosė.........
Ed_Bunker
13-08-2005, 13:20
non devi fare rh.run(), ma rh.start().
I thread si avviano cosė.........
Ma quanto saro' pirla !
E il bello e' che il secondo thread lo avvio correttamente... :eek:
P.S.: grazie ;)
franksisca
13-08-2005, 19:05
di nulla, capita.
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.