我很头疼用java杀死一个线程......我在stackoverflow上看到了很多主题,但我没有让他们处理我的代码......有人可以解释一下我如何能够在不使用deprecated的情况下杀死一个线程函数(如停止),请以安全的方式(我的线程也在运行一个套接字:DatagramSocket)。
类 p2p_app->
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
//import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class p2p_app {
private String ip;
private Integer porta;
private LinkedList<Vizinho> vizinhos;
private String pathSharedFolder;
private String pathBootStrap;
private int exit;
//private Thread send;
//private Thread receive;
private UDPreceive udpR;
public p2p_app(String args[]) throws IOException {
this.ip = InetAddress.getLocalHost().getHostAddress();
this.vizinhos = new LinkedList<Vizinho>();
this.exit = 0;
//this.send=null;
//this.receive=null;
this.udpR=null;
if(args.length==2){
this.pathSharedFolder=args[0];
this.pathBootStrap=args[1];
System.out.println(pathSharedFolder);
System.out.println(pathBootStrap);
}
else{
this.pathSharedFolder="./";
this.pathBootStrap="./p2p_bootstrap.conf";
System.out.println(pathSharedFolder);
System.out.println(pathBootStrap);
}
readFile(this.pathBootStrap);
createSharedFolder(this.pathSharedFolder);
}
public void assign(String tipo,String info) //tratar o file bootstrap.conf
{
Tipos currentTipos = Tipos.valueOf(tipo.toUpperCase());
switch(currentTipos){
case PATH: if(this.pathSharedFolder==null)
this.pathSharedFolder = info;
break;
case PORTA: this.porta = Integer.parseInt(info);
break;
case IP: StringTokenizer st = new StringTokenizer(info,":");
st.nextElement();
String[] tokens = info.split(":");
Vizinho s = new Vizinho(tokens[0],Integer.parseInt(tokens[1]));
this.vizinhos.add(s);
break;
default:
break;
}
}
public void trataLine(String line){
Pattern p = Pattern.compile("[\\w\\./:]+");
Matcher m = p.matcher(line);
String tipo = "";
while(m.find()){
if(tipo.compareTo("")==0)
tipo = m.group();
else assign(tipo,m.group());
}
}
public void readFile(String path) throws IOException{ //modifiquei este codigo para ver se existe ou nao o ficheiro bootstrap (VASCO)
String line;
Pattern p = Pattern.compile("\\$");
File f = new File(path);
if(f.exists()){
BufferedReader br;
br = new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null) {
Matcher m = p.matcher(line);
if(m.find() == true)
trataLine(line);
}
br.close();
}
else{
System.out.println("FILE :: BOOTSTRAP.CONF : Doesn't exist.");
}
}
public void createSharedFolder(String path) {
if(!(new File(path).exists()))
new File(path).mkdir();
}
public enum Tipos {
PATH,
PORTA,
T1,
T2,
T3,
R,
M,
K,
IP
}
public String getIp(){
return this.ip;
}
public Integer getPorta(){
return this.porta;
}
public int getExit(){
return this.exit;
}
public void setExit(int exit){
this.exit = exit;
}
public LinkedList<Vizinho> getVizinhos(){
LinkedList<Vizinho> aux = new LinkedList<Vizinho>();
for(Vizinho c : this.vizinhos) aux.add(c);
return aux;
}
public String toString(){
StringBuilder s = new StringBuilder();
s.append("IP:"+this.ip + "\n");
s.append("Porta:"+ this.porta +"\n");
s.append("Directory:" + this.pathSharedFolder + "\n");
s.append("-----Vizinhos-----");
for(Vizinho c : this.vizinhos)
s.append(c.toString());
return s.toString();
}
public void initThreads(p2p_app p2p){
//UDPreceive udpR = new UDPreceive(p2p);
this.udpR = new UDPreceive(p2p);
//UDPsend udpS = new UDPsend(p2p);
//this.receive = new Thread(udpR);
Thread t = new Thread(udpR);
//this.send = new Thread(udpS);
t.start();
//this.receive.start();
//this.send.start();
}
@SuppressWarnings("deprecation")
public void stopThreads(){
this.udpR.stopRun();
//this.receive.interrupt();
//this.receive.stop();
//this.receive.toString();
//this.send.interrupt();
//this.send.toString();
}
public void menu(){
System.out.println("1:Hello");
System.out.println("2:Vasco");
System.out.println("3:Exit");
}
public int choiceMenu(int i){
int numRowsInConsole = 60;
final String ESC = "\033[";
switch(i){
case 1:
System.out.println("FUNCIONOU HELLO");
System.out.print(ESC + "2J");
/*for (int ii=0; ii<numRowsInConsole; ii++) {
// scroll down one line
System.out.println("");
}*/
break;
case 2:
System.out.println("FUNCIONOU VASCO");
System.out.print(ESC + "2J");
break;
case 3:
i=-1;
System.out.print(ESC + "2J");
break;
default:
}
return i;
}
public static void main(String[] args) throws IOException {
int i;
p2p_app p2p = new p2p_app(args);
//p2p.initThreads(p2p);
System.out.println(p2p.toString());
Scanner sc = new Scanner(System.in);
while(p2p.getExit() != -1){
p2p.menu();
i = sc.nextInt();
p2p.setExit(p2p.choiceMenu(i));
System.out.println(p2p.getExit());
}
System.out.println("Woot woot!");
//p2p.stopThreads();
}
}
类 UDPreceive->
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UDPreceive implements Runnable {
private p2p_app p2p;
private DatagramPacket p;
public volatile boolean stopThread = true;
public void stopRun(){
this.stopThread=false;
}
public UDPreceive(p2p_app p2p){
this.p2p = p2p;
}
/**
* @param args
*/
public void run(){
DatagramSocket socket=null;
UDPpacket udp;
byte[] x = new byte[1000];
try{
socket = new DatagramSocket(8734);
socket.setBroadcast(true);
//while(this.p2p.getExit() !=-1){
while(stopThread){
p = new DatagramPacket(x,x.length);
socket.receive(p);
udp = new UDPpacket(p,this.p2p);
udp.tostring();
//udp.setDatagramPacket(p);
//String d = new String(p.getData());
//System.out.println("Mensagem enviada por mim: "+d);
}
//Thread.sleep(100);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我如何能够在 p2p_app 类中杀死我的主函数上的线程?我为我的 UDPreceiver 类创建了一个线程:F