Scusate il doppio post, ma sono incappato in un nuovo problema: in questo codice (che il server postato in precedenza modificato) dichiaro "public synchronized void run() { ... }", quindi teoricamente in quella parte di codice dovrebbe entre un thread alla volta finchè non esce giusto? Invece nell'esecuzione vedo che gli n thread che faccio partire entrano quando vogliono loro in run(), come se synchronized non ci fosse... come mai??
codice:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
/**
* This is to help people to write Client server application
* I tried to make it as simple as possible... the client connect to the server
* the client send a String to the server the server returns it in UPPERCASE thats all
*/
public class Server {
public static int i=1;
public static double[][] vettore = new double[i][10];
public static double[] var1 = new double[i];
public static int numt = 0;
// the socket used by the server
private ServerSocket serverSocket;
public int j=0;
// server constructor
Server(int port){
/* create socket server and wait for connection requests */
try
{
serverSocket = new ServerSocket(port);
System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
while(true)
{
Socket socket = serverSocket.accept(); // accept connection
System.out.println("New client asked for a connection");
for(j=0; j<i; j++){
TcpThread t = new TcpThread(socket, j); // make a thread of it
// System.out.println("Starting thread 1");
// TcpThread t2 = new TcpThread(socket, 2); // make a thread of it
// System.out.println("Starting thread 2");
// TcpThread t3 = new TcpThread(socket, 3); // make a thread of it
// System.out.println("Starting thread 3");
System.out.println("Starting thread " + j);
t.start();
// t2.start();
// t3.start();
}
}
}
catch (IOException e) {
System.out.println("Exception on new ServerSocket: " + e);
}
}
// you must "run" server to have the server run as a console application
public static void main(String[] arg) {
// start server on port 1500
new Server(1500);
}
/** One instance of this thread will run for each client */
class TcpThread extends Thread {
// the socket where to listen/talk
int n;
Socket socket;
DataInputStream Sinput;
DataOutputStream Soutput;
TcpThread(Socket socket, int n) {
this.socket = socket;
this.n = n;
}
public synchronized void run() {
// synchronized (this){
numt++;
/* Creating both Data Stream */
System.out.println("Thread " + n + " trying to create Object Input/Output Streams");
try
{
// create output first
Soutput = new DataOutputStream(socket.getOutputStream());
// Soutput.flush();
Sinput = new DataInputStream(socket.getInputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
System.out.println("Thread " + n + " waiting for a String from the Client");
// read a String (which is an object)
try {
// creazione vettore di interi
for(int j=0; j<10; j++){
vettore[n][j] = Math.random()*Integer.MAX_VALUE;
}
// individuazione del valore var1[n]
for(int j=0; j<10; j++){
if(j==0)
var1[n]=vettore[n][j];
if(var1[n]>vettore[n][j])
var1[n]=vettore[n][j];
}
// mi faccio inviare il var2 dal professor
double var2 = (double) Sinput.readDouble();
System.out.println("Ricevuto var2 = " + var2);
// se il mio var1 è > del suo var2 allora glielo invio...
if(var1[n]>var2){
double kessvar=1;
System.out.println("Avviso il Professor che gli dto per inviare var1");
Soutput.writeDouble(kessvar);
System.out.println("var1[n] = " + var1[n] + " > var2 = " + var2 + " => INVIO\n\n");
Soutput.writeDouble(var1[n]);
}else{
// ...altrimenti comunico che non invio niente
double kessvar=0;
System.out.println("Avviso il Professor che rimango in attesa");
Soutput.writeDouble(kessvar);
System.out.println("var1[n] = " + var1[n] + " < var2 = " + var2 + " => NON INVIO\n\n");
// Soutput.writeline("NON INVIO UN BEL NIENTE");
}
}
catch (IOException e) {
System.out.println("Exception reading/writing Streams: " + e);
return;
}
// will surely not happen with a String
// catch (ClassNotFoundException o) {
// }
if(numt==i){
try {
Soutput.close();
Sinput.close();
}catch (Exception e) {}
numt=0;
}
// }
}
/* public synchronized void exit(){
int newValue = c - 1;
c = newValue;
try {
Soutput.close();
Sinput.close();
}catch (Exception e) {}
} */
}
/* class funzione {
synchronized void printThis(char c, int times) {
for (int j=0; j<times; j++) {
try{Thread.sleep(1);}
catch(Exception ex) {ex.printStackTrace();}
System.out.print(c);
}
}
} //end class */
}