How can I make a Java thread always run before another thread?
pIn my program, when a user enters a number, the program sends that number
to the server through sockets and the server sends back data matching that
number. The number represents a service level. The thread which has the
IncomingReader() instance as its runnable then reads what was sent from
the server, stores it an arraylist(details). I then create objects of
class MyClients using the data in the details arraylist. My problem is
that the loop which creates the objects runs before the thread that reads
data from the server runs. How can I make the thread that reads from the
server run before the loop that creates the objects? The code is as
follows: (I've removed the code for the GUI for the sake of being
concise)/p precodepublic class SearchClients { JFrame frame; private
JTextField textField; private JTextField textField_1; private JTextField
textField_2; private JTextField textField_3; BufferedReader reader;
PrintWriter writer; Socket sock; static ArrayListlt;Stringgt; details =
new ArrayListlt;Stringgt;(); public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { public void run() { try {
SearchClients window = new SearchClients(); window.frame.setVisible(true);
} catch (Exception e) { e.printStackTrace(); } } }); } public
SearchClients() { initialize(); } private void initialize() {
setUpNetworking(); Thread readerThread = new Thread(new IncomingReader());
readerThread.start(); JButton btnSearchByService = new JButton(Search By
Service Level); btnSearchByService.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent arg0) { searchByServiceLevel();
} }); } public void searchByServiceLevel() { try { writer.println(SEARCH
BY SERVICE LEVEL); writer.println(textField_1.getText()); writer.flush();
} catch (Exception ex) { ex.printStackTrace(); } JPanel nameSearchPane =
new JPanel(new BorderLayout());
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setContentPane(nameSearchPane); frame.invalidate();
frame.validate(); String[] columns = {Name, Phone Number, Address,
District, County, Village, Installation Date, Energy Store, Service Level,
Account Balance, Months Owed, Next Payment Date}; ArrayListlt;MyClientsgt;
clientDetails = new ArrayListlt;MyClientsgt;(); for(int y = 0; y lt;
details.size(); y++) { MyClients client = new MyClients(); client.name =
details.get(y); client.phone_number = details.get(++y); client.address =
details.get(++y); client.district = details.get(++y); client.county =
details.get(++y); client.village = details.get(++y);
client.installation_date = details.get(++y); client.energy_store =
details.get(++y); client.service_level = details.get(++y);
client.next_payment_date = details.get(++y); client.account_balance =
details.get(++y); client.months_owed = details.get(++y);
clientDetails.add(client); } details.clear(); // Check if any data was
returned from the database if(clientDetails.isEmpty()) {
JOptionPane.showMessageDialog(frame, A client with that service level was
not found.\n Try service level: 1, 2, 3 or 4.); frame.setVisible(false);
SearchClients search = new SearchClients(); search.frame.setVisible(true);
} String[][] clients = new String[100][100]; for(int x = 0; x lt;
clientDetails.size(); x++) { clients[x][0] = clientDetails.get(x).name;
clients[x][1] = clientDetails.get(x).phone_number; clients[x][2] =
clientDetails.get(x).address; clients[x][3] =
clientDetails.get(x).district; clients[x][4] =
clientDetails.get(x).county; clients[x][5] = clientDetails.get(x).village;
clients[x][6] = clientDetails.get(x).installation_date.toString();
clients[x][7] = clientDetails.get(x).energy_store; clients[x][8] =
clientDetails.get(x).service_level; clients[x][9] =
clientDetails.get(x).account_balance; clients[x][10] =
clientDetails.get(x).months_owed; clients[x][11] =
clientDetails.get(x).next_payment_date.toString(); } JTable table = new
JTable(clients, columns); JScrollPane tableContainer = new
JScrollPane(table); nameSearchPane.add(tableContainer,
BorderLayout.CENTER); } private void setUpNetworking() { try { sock = new
Socket(127.0.0.1, 5000); InputStreamReader streamReader = new
InputStreamReader( sock.getInputStream()); reader = new
BufferedReader(streamReader); writer = new
PrintWriter(sock.getOutputStream()); System.out.println(Networking
established); } catch (IOException ex) { ex.printStackTrace(); } } class
IncomingReader implements Runnable { public void run() { String message;
try { while ((message = reader.readLine()) != null) {
details.add(message); } } catch (IOException ex) { ex.printStackTrace(); }
} } class MyClients { String name = ; String phone_number = ; String
address = ; String district = ; String county = ; String village = ;
String installation_date = ; String energy_store = ; String service_level
= ; String next_payment_date = ; String account_balance = ; String
months_owed = ; String clientID = ; } } /code/pre
No comments:
Post a Comment