Problem
Hello!
I have a problem with my code.
I am trying to store the data that i read in the run method of SocketHandler in a singleton class.
But when i try to display it in the findMeeting method it is empty.
I cant get it out of the run method by returning it eighter because the runmethod returns void.
What is wrong in my code?
Can i get out the data from the run method in another way?
import java.net.*;
import java.io.*;
import java.util.*;
publicclass Meeting
{
publicstaticvoid main(String a[])throws IOException
{
new Meeting().listen();
new Meeting().checkTimes(a);
new Meeting().findMeeting();
}
publicvoid listen()throws IOException
{
new ServerSocketHandler().start();
}
publicvoid checkTimes(String a[])throws IOException
{
//for(int i = 1; i < Integer.parseInt(a[0]); i++)
//new SocketHandler(i).start();
new SocketHandler(1).start();
}
publicvoid findMeeting()
{
LinkedList<String> times =new LinkedList<String>();
TimeList timeList = TimeList.getInstance();
times = timeList.getTimes();
int timesSize = times.size();
for(int i = 0; i < timesSize; i++)
System.out.println(times.get(i));
}
}
class ServerSocketHandlerextends Thread
{
int port = 4000;
publicvoid run()
{
try
{
int timesSize = 0;
List times =new LinkedList();
ServerSocket serverSocket =new ServerSocket(port);
Socket socket = serverSocket.accept();
PrintWriter pw =new PrintWriter(socket.getOutputStream(),true);
try
{
RandomAccessFile f =new RandomAccessFile("tider.dat","r");
try
{
f.seek(0);
while(true)
{
times.add(f.readUTF());
timesSize++;
}
}
catch(IOException ioe)
{
// System.out.println("\nEnd of file.");
}
finally
{
try
{
f.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
catch(FileNotFoundException fnfe)
{
System.out.println(fnfe);
}
pw.println(timesSize);
for(int i = 0; i < timesSize; i++)
{
pw.println(times.get(i));
pw.flush();
}
pw.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
class SocketHandlerextends Thread
{
int port;
LinkedList<String> times =new LinkedList<String>();
SocketHandler(int i)
{
//this.port = 4000 + i;
this.port = 4000;
}
publicvoid run()
{
try
{
Socket socket =new Socket("localhost", this.port);
InputStream is = socket.getInputStream();
BufferedReader br =new BufferedReader(new InputStreamReader(is));
int timesSize = Integer.parseInt(br.readLine());
for(int i = 0; i < timesSize; i++)
times.add(br.readLine());
TimeList timeList = TimeList.getInstance();
timeList.setTimes(times);
br.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
class TimeList
{
privatestatic TimeList instance =new TimeList();
private LinkedList<String> times =new LinkedList<String>();
private TimeList(){}
publicstatic TimeList getInstance()
{
return instance;
}
publicvoid setTimes(LinkedList<String> t)
{
this.times = t;
}
public LinkedList<String> getTimes()
{
return this.times;
}
}

