I've a problem. I'm trying to create a thread for communcation with a tcp server. My problem is that i need to shutdown the thread. How can I do this? I tried .interrupt, .stop, and that didn't work.
Best Regards
Chat thread = new Chat(socket, reuniao[0], username);
public class Chat implements Runnable{
Socket socket;
boolean corre;
String reuniao, username;
BufferedReader input;
PrintWriter out;
Scanner sc;
public Chat(Socket socket, String reuniao, String username) {
this.socket = socket;
this.reuniao = reuniao;
this.username = username;
corre = true;
new Thread(this, "").start();
}
@Override
public void run() {
sc = new Scanner(System.in);
try {
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
out = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String answer = null;
out.println("Download Chat#"+reuniao);
try {
answer = input.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("I'm ready!");
ArrayList< String > salaChat = new ArrayList< String >();
String [] chatCompleto = answer.split("#");
for(int i = 0; i < chatCompleto.length; i++){
salaChat.add(chatCompleto[i]);
System.out.println(chatCompleto[i]);
}
while(!Thread.currentThread().isInterrupted()){
if(corre == false) return;
out.println("Download Chat#"+reuniao);
try {
answer = input.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String [] temp = answer.split("#");
for(int i = 0; i < temp.length; i++){
int testa = 0;
for(int j = 0; j < salaChat.size(); j++){
if(temp[i].equals(salaChat.get(j))){
testa = 1;
}
}
if(testa == 0){
salaChat.add(temp[i]);
String [] aux = temp[i].split(":");
if(!username.equals(aux[0]) && temp[i] != null){
System.out.println(temp[i]);
}
}
}
}
}
}