Witam. Dopiero zaczynam w javie i zapewne ten problem jest strasznie blachy...jednak ja juz nie mam pomyslu [???] . Chodzi mi o sprawdzenie,czy watek jest uruchomiony ( isAlive() ). Czy trzeba odwolac,sie do tego watku 'po nazwie' czy moze 'id'? poniewaz kod ponizej (tu w wersji okrojonej nie dziala) nie dziala. Po prostu musze stworzyc warunek,ze jesli bedziemy chcieli edytowac plik z adresami ip', a akurat w tym momencie bedzie trwal 'watek polaczenia z klientami',to ma wysiwietlic sie komunikat o bledzie. Zapewne jest to banal,wiec mam nadzieje,ze pomozecie :-P .
public class Serwer extends JFrame implements ActionListener, Runnable, MouseListener{
Watek_polaczenia1 wpol1;
public Serwer(){
wpol1 = new Watek_polaczenia1(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==wyslij1){
new Thread(wpol1,"pol").start();
ramka.dispose();
}
if(e.getActionCommand().equals("Enter IP") && Thread.currentThread().getName().equals("pol"))
JOptionPane.showMessageDialog(null, "Nie mozna zapisywac do pliku,trwa laczenie!!!");
else if(e.getActionCommand().equals("Enter IP")){
ip_adresses();
}
public class Watek_polaczenia1 implements Runnable{
Socket klient1, klient2, klient3, klient4,klient5;
DataInputStream wejscie;
DataOutputStream wyjscie;
String msg;
File plik;
Serwer serwerClass;
Client1Thread c1;
Client2Thread c2;
Client3Thread c3;
Client4Thread c4;
Client5Thread c5;
Icon gif_T = new ImageIcon("jest.gif");
Icon gif_F = new ImageIcon("stop.gif");
Icon gif_N = new ImageIcon("neutral.gif");
boolean flaga=true;
Object line1,line2,line3,line4,line5;
public Watek_polaczenia1(Serwer serwerClass) {
this.serwerClass = serwerClass;
c1 = new Client1Thread(this, serwerClass);
c2 = new Client2Thread(this, serwerClass);
c3 = new Client3Thread(this, serwerClass);
c4 = new Client4Thread(this, serwerClass);
c5 = new Client5Thread(this, serwerClass);
}
public void run(){
int i = 0;
do{
i++;
System.out.println("watek pol1 powtarza co okreslony czas(nr"+i+")\n");
polaczenie1();
}while(!flaga);
}
public void polaczenie1(){
msg = serwerClass.pole1.getText();
plik = new File("f:/dane.txt");
ArrayList file = new ArrayList();
String line = null;
try
{
BufferedReader in = new BufferedReader(new FileReader(plik));
while ((line = in.readLine()) != null)
file.add(line);
line1 = file.get(0);
line2 = file.get(1);
line3 = file.get(2);
line4 = file.get(3);
line5 = file.get(4);
System.out.println(line1);
System.out.println(line2);
System.out.println(line3);
System.out.println(line4);
System.out.println(line5);
in.close();
}
catch (IOException e)
{
System.out.println(e);
}
//pierwszy powiadamiany
try{
klient1 = new Socket(line1.toString(),1234);
//wejscie = new DataInputStream(new BufferedInputStream(klient1.getInputStream()));
wyjscie = new DataOutputStream(new BufferedOutputStream(klient1.getOutputStream()));
try{
wyjscie.writeUTF(msg);
}catch(NullPointerException e){
}
wyjscie.close();
klient1.close();
if(klient1.isConnected())
System.out.println("Polaczyles sie z " +klient1);
serwerClass.icon1.setIcon(gif_T);
}catch(IOException e){
System.out.println("Polaczenie nie udalo sie" +line1.toString());
serwerClass.icon1.setIcon(gif_F);
new Thread(c1).start();
}
//drugi powiadamiany
try{
klient2 = new Socket(line2.toString(),1234);
//wejscie = new DataInputStream(new BufferedInputStream(klient1.getInputStream()));
wyjscie = new DataOutputStream(new BufferedOutputStream(klient2.getOutputStream()));
try{
wyjscie.writeUTF(msg);
}catch(NullPointerException e){
}
wyjscie.close();
klient2.close();
if(klient2.isConnected())
System.out.println("Polaczyles sie z " +klient2);
serwerClass.icon2.setIcon(gif_T);
}catch(IOException e){
System.out.println("Polaczenie nie udalo sie" +klient2);
serwerClass.icon2.setIcon(gif_F);
new Thread(c2).start();
}
}
............................................................................
//itd