Newsgroup: it.comp.java
Titolo: Accept non sbloccabile!?

Salve,
volevo sapere se esiste un modo per sbloccare un Thread che è fermo su 
una accept() (quella della classe serversocket...)
nei documenti in cui ho cercato c'e' scritto  sempre che la accept manda 
in stato di blocco, in realtà pero' è un stato di blocco particolare 
perche' : non rilascia la risorsa ,se è inserito in un metodo 
synchronized, e non permette di essere stoppato con il metodo interrup() 
o stop()!.
Come devo fare se voglio sbloccarlo? Esiste un metodo pulito per farlo?
Grazie mille!
ciao

,

------------------------ Metodo A --------------------------
import java.io.*;
import java.net.*;
public class ServerSocketTestA extends Thread
{
	private int port;
	private ServerSocket ss;
	public boolean repeat = true;
	public boolean running = true;
	public ServerSocketTestA (int port)
	{
		this.port=port;
		setDaemon(true);
		start();
	}
	public void run()
	{
		try
		{
			ss = new ServerSocket(port);
			ss.setSoTimeout(2000);
			int i=0;
			while(repeat)
			{
				System.out.println("Listening... ("+
(i++)+")");
				try
				{
					Socket s = ss.accept();
					new EchoThread(s);
				}
				catch (InterruptedIOException ex) {}
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			running = false;
		}
	}
	public void doStop()
	{
		repeat = false;
	}
	public boolean isRunning()
	{
		return running;
	}
	public static void main(String args[]) throws Exception
	{
		ServerSocketTestA sst = new ServerSocketTestA(9999);
		System.out.println("Server started! Hit enter to stop
it.\n");
		System.in.read();
		System.out.println("Server scheduled to stop...");
		sst.doStop();
		while(sst.isRunning())
			Thread.sleep(200);
		System.out.println("Server stopped!");
		System.exit(0);
	}
}
class EchoThread extends Thread
{
	InputStream is;
	OutputStream os;
	public EchoThread(Socket s) throws IOException
	{
		is = s.getInputStream();
		os = s.getOutputStream();
		setDaemon(true);
		start();
	}
	public void run()
	{
		int c;
		try
		{
			System.out.println("Echo thread "+this+"
started");
			while((c=is.read()) != -1)
				os.write(c);
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
		System.out.println("Echo thread "+this+" stopped");
	}
}
------------------------ Metodo B --------------------------
import java.io.*;
import java.net.*;
public class ServerSocketTestB extends Thread
{
	private int port;
	private ServerSocket ss;
	public boolean stopRequested = false;
	public ServerSocketTestB (int port)
	{
		this.port=port;
		setDaemon(true);
		start();
	}
	public void run()
	{
		try
		{
			ss = new ServerSocket(port);
			System.out.println("Listening... ");
			Socket s = ss.accept();
			new EchoThread(s);
		}
		catch (Exception ex)
		{
			if (!stopRequested)
				ex.printStackTrace();
		}
	}
	public void doStop()
	{
		stopRequested = true;
		try {ss.close();}
		catch (Exception ex) {}
	}
	public static void main(String args[]) throws Exception
	{
		ServerSocketTestB sst = new ServerSocketTestB(9999);
		System.out.println("Server started! Hit enter to stop
it.\n");
		System.in.read();
		sst.doStop();
		System.out.println("Server stopped!");
	}
}
class EchoThread extends Thread
{
	InputStream is;
	OutputStream os;
	public EchoThread(Socket s) throws IOException
	{
		is = s.getInputStream();
		os = s.getOutputStream();
		setDaemon(true);
		start();
	}
	public void run()
	{
		int c;
		try
		{
			System.out.println("Echo thread "+this+"
started");
			while((c=is.read()) != -1)
				os.write(c);
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
		System.out.println("Echo thread "+this+" stopped");
	}
}
Sent via Deja.com 
http://www.deja.com/
Before you buy.

[...]
No. :)
-- 
God is Real, unless declared Integer.
ObjectZone - 
http://space.tin.it/computer/csadun/