how to stop a thread?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   A_D
Posted On:   Friday, March 17, 2006 06:45 PM

hi i've just solved to pass a reference from class a to b and vive-versa by invoking tho methods from main which have a reference one from another this is with the purpose of the thread "A" can stop the thread "B" and "B" can stop "A" by sending a message from b (shutdown) but the problem is because stop(); is deprecated and interrupt(); doesn't work so another doubt has arisen to me that's why the new question is....... how can i stop both threads in a well-behaved application?? (avoiding System.exit ( 0 ) of course) by implementing another methods instead of these???????????????????????' here's my source Many Thanks A D //THIS IS MAIN THREAD    More>>

hi i've just solved to pass a reference from class a to b and vive-versa by invoking tho methods from main which have a reference one from another this is with the purpose of the thread "A" can stop the thread "B" and "B" can stop "A"
by sending a message from b (shutdown) but the problem is because stop(); is deprecated and interrupt(); doesn't work so another doubt has arisen to me that's why the new question is....... how can i stop both threads in a well-behaved application?? (avoiding System.exit ( 0 ) of course) by implementing another methods instead of these???????????????????????'
here's my source

Many Thanks

A D

			



//THIS IS MAIN THREAD




import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.ConnectException;

public class ClientEx
{

/*_____________________________________________________________________________
*
*
* M A I N
*
*___________________________________________________________________________*/

public static void main ( String argv [ ] ) throws IOException
{

boolean connection = true;

ClientExThread connect = null;

ClientExThread2 connect2 = null;

Socket socketClient = null;

String host = null;

int port = 0,
i = 0,
j = 0;

/*---command-line arguments---*/
if ( argv.length != 4)
{
System.err.println ("usage: java ClientEx -ip_address < > " +
"-port <#> ");

System.exit ( 0 );
}
else
for ( i = 0; i < argv.length; i++ )

{
if( argv [ i ].compareTo ( "-ip_address" ) == 0 )
{
host = ( argv [ i+1 ]);
}
else if( argv [ i ].compareTo ( "-port" ) == 0 )
{
try
{
port = Integer.parseInt ( argv [ i+1 ] );
}
catch ( NumberFormatException e )
{
System.err.println ("

port must be in number: " +
e.toString ( ));
System.err.println ("usage: java ClientEx -ip_address < > " +
"-port <#> ");
System.exit ( 0 );
}
}
}
if ( port < 1 || port > 65535 )
{
System.err.println ("
port must be between 1 and 65535

" +
"usage: java ClientEx -ip_address < > " +
"-port <#> ");
System.exit ( 0 );
}

for (i = 0; i < 4; i += 2)

{
if ( argv[ i ].compareTo ( "-ip_address" ) != 0 &&
argv[ i ].compareTo ( "-port" ) != 0 )
{
System.err.println ("

invalid option: '" +argv [ i ]+"'
" +
"usage: java ClientEx -ip_address < > " +
"-port <#> ");

System.exit ( 0 );
}
}
for (i = 2; i >= 0; i -=2)
{
for (j = 0; j <= i-1; j +=2)
if( argv [ i ].compareTo (argv [ j ]) == 0)
{
System.err.println ("

duplicated option: '" +
argv [ i ]+"'
" +
"usage: java ClientEx -ip_address < > " +
"-port <#> ");

System.exit ( 0 );
}
}

try
{

socketClient = new Socket( host ,port );

System.out.println("
client connected at port: " +
socketClient.getLocalPort ( ) +
" and at the address: " +
socketClient.getInetAddress( ));
}
catch ( UnknownHostException e)
{
System.err.println("IP of host couldn't be determined" +
e.toString ( ));
System.exit ( 0 );
}
catch ( ConnectException e )
{
System.err.println ("

Be sure that the port be the " +
"same as the server is running otherwise " +
"if is a server running
" +
e.toString ( ));
System.exit ( 0 );
}

/*--- read from keyboard ----*/
connect = new ClientExThread ( socketClient );

/*--- read from socket ---**/
connect2 = new ClientExThread2 ( socketClient );

connect.Setter1 ( connect2 );
connect2.Setter2 ( connect );

connect.start ();
connect2.start ();


System.err.println ("Saliendo del main....");

}/*____________________________END OF MAIN_________________________________*/

}/*_________________________END OF ClientEx Class____________________________*/

/*===========================================================================*/













			

//THIS IS THE THREAD1 CONTROLS THE KEYBOARD
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;

public class ClientExThread extends Thread
{

private volatile ClientExThread2 client2 = null;

private Socket client1 = null;

private boolean sending = true;

private BufferedReader StdIn = null;

private PrintWriter output = null;

/*_____________________________________________________________________________
*
*
* CONSTRUCTOR
*
*___________________________________________________________________________*/

public ClientExThread( Socket client_conn1 )
{
client1 = client_conn1;
}

/*_____________________________________________________________________________
*
*
* SETTER
*
*___________________________________________________________________________*/

public void Setter1 ( ClientExThread2 thread2 )
{
client2 = thread2;
System.out.println ( "REFERENCE PASSED Thread 1" );
}
/*--------------------------end Setter------------------------------------*/

/*_____________________________________________________________________________
*
*
* R U N
*
*___________________________________________________________________________*/




public void run()
{
String stdin = null;

try
{
output = new PrintWriter (client1.getOutputStream ( ) ,true );
}

catch ( IOException e)
{
e.printStackTrace();
}

try
{
StdIn = new BufferedReader(new InputStreamReader( System.in ));

while ( sending )
{
stdin = StdIn.readLine();
output.println ( stdin );

if ( stdin.compareToIgnoreCase ( "exit" ) == 0 )
{
client1.close();
sending = false;
}
else
if( stdin.compareToIgnoreCase ( "shutdown" ) == 0 )
{
client2.stop();// <---------WHAT SHOULD USE INSTEAD OF THIS?????????'


sending = false;
}
}
}
catch (SocketException e )
{
System.err.println ("

The a socket has been closed!!!!!" +
" Message couldn't sent ");
}
catch ( IOException e )
{
e.printStackTrace();
}

System.err.println ("EXITING ClientExTread.run ...");

}/*--------------------------end run---------------------------------------*/
}/*--------------------------end ClientExThreadclass-------------------------*/

/*===========================================================================*/













			
// THIS IS THE THREAD 2 RECEIVES THE STRING
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;


public class ClientExThread2 extends Thread
{

private volatile ClientExThread client_1 = null;

private Socket client_2 = null;

private BufferedReader in = null;

private boolean send_str = true;

/*_____________________________________________________________________________
*
*
* CONSTRUCTOR
*
*___________________________________________________________________________*/
public ClientExThread2( Socket client_conn )
{
client_2 = client_conn;
}

/*_____________________________________________________________________________
*
*
* SETTER
*
*___________________________________________________________________________*/
public void Setter2 ( ClientExThread thread1 )
{
client_1 = thread1;
System.out.println ( "REFERENCE PASSED Thread 2" );

}/*-------------------------- End setter ----------------------------------*/

/*_____________________________________________________________________________
*
*
* R U N
*
*___________________________________________________________________________*/




public void run()
{

BufferedReader in = null;
boolean send_str = true;
String receiving_str = null;
try
{
in = new BufferedReader
(new InputStreamReader( client_2.getInputStream( ) ) );
}

catch ( IOException e)
{
e.printStackTrace();
}

try
{
while ( send_str )
{
receiving_str = in.readLine();
System.out.println( receiving_str );

if( receiving_str.compareToIgnoreCase ( "shutdown" ) == 0 )
{
client_1.stop();// <------- WHAT SHOULD I USE INSTEAD OF THIS?????????????????????????????
send_str = false;

}
}
}
catch (SocketException e )
{
System.err.println ("
client closed ");

}
catch ( IOException e )
{

e.printStackTrace();
}
}/*------------------------------end run-----------------------------------*/

}/*--------------------------end ClientExThread2class------------------------*/

/*===========================================================================*/

   <<Less

Re: how to stop a thread?

Posted By:   Edward_Harned  
Posted On:   Tuesday, March 21, 2006 01:00 PM

 


In each thread class:



Add field:

    private volatile boolean ending = false;


In run() method:

    last line of while (sending) loop

    add:

        if (ending) sending = false;


Add a method to each thread class:

    public synchronized shutDown() {


        ending = true:


    }


 


Re: how to stop a thread?

Posted By:   WarnerJan_Veldhuis  
Posted On:   Saturday, March 18, 2006 03:25 PM

The way to stop a thread is to let it "die", that is: let run() end. You should create a method in class ClientExThread called stopSending(), that sets sending = false. That way the infinite loop in the run() will come to an end, and the run() methods is finished...
About | Sitemap | Contact