【发布时间】:2014-04-28 20:18:00
【问题描述】:
此程序使用客户端发送Computer 对象(属性品牌、价格、数量),服务器将总费用返回给客户端。它必须能够连续运行将转发线程发送到服务器的循环。
但是,在第二个线程应该完成后,程序停止。我需要弄清楚如何保持它运行,谢谢。附加的类是Computer、ComputerServer w/HandleAClient 和ComputerClient。我为编辑道歉,我仍在学习如何使用它。
import java.io.Serializable;
public class Computer implements Serializable
{
private String brand;
private double price;
private int quantity;
public Computer()
{
setBrand("");
setPrice(0.0);
setQuantity(0);
}
public Computer(String b, double p, int q)
{
setBrand(b);
setPrice(p);
setQuantity(q);
}
public String getBrand()
{
return brand;
}
public double getPrice()
{
return price;
}
public int getQuantity()
{
return quantity;
}
public void setBrand(String b)
{
brand = b;
}
public void setPrice(double p)
{
price = p;
}
public void setQuantity(int q)
{
quantity = q;
}
public String toString()
{
return("Brand: "+brand+"\t"+"Price: "+price+"\t"+"Quantity: "+quantity);
}
}
import java.util.*;
import java.io.*;
import java.net.*;
public class ComputerClient
{
public static void main(String args[])
{
Socket connection;
Scanner scanner = new Scanner(System.in);
Scanner quantity = new Scanner(System.in);
Scanner price = new Scanner(System.in);
Scanner brand = new Scanner(System.in);
ObjectOutputStream output;
ObjectInputStream input;
String b;
double p;
int q;
Object obj;
try
{
int exit= 1;
connection = new Socket("localhost",8000);
output = new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
while(exit!=-1)
{
System.out.println("Please Enter a Computer Brand\n");
b = brand.nextLine();
System.out.println("Please Enter the Price\n");
p = price.nextDouble();
System.out.println("Please Enter the Quantity\n");
q = quantity.nextInt();
Computer c = new Computer(b,p,q);
output.writeObject(c);
output.flush();
//read back:
obj=(Object)input.readObject();
System.out.println(obj.toString());
System.out.println("Press any Integer to Continue, To Exit Press -1");
exit = scanner.nextInt();
}
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class ComputerServer
{
public static void main(String args[])
{
ServerSocket serverSocket;
Socket connection;
ObjectInputStream input;
ObjectOutputStream output;
Computer c = null;
Object obj;
double totalCharge;
try
{
serverSocket = new ServerSocket(8000);
System.out.println("Waiting for Client");
int clientNo = 1;
ExecutorService threadExecutor = Executors.newCachedThreadPool();
while(true)//runs indefinitely
{
connection = serverSocket.accept();
input = new ObjectInputStream(connection.getInputStream());
output = new ObjectOutputStream(connection.getOutputStream());
obj = input.readObject();
System.out.println("\nObject Received from Client:\n"+obj);
if(obj instanceof Computer)
{
totalCharge = ((Computer)obj).getPrice()*((Computer)obj).getQuantity();
HandleAClient thread = new HandleAClient(connection, clientNo, totalCharge);
threadExecutor.execute(thread);
output.writeObject(totalCharge);
output.flush();
}
clientNo++;
}
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}//end of main
}
class HandleAClient implements Runnable
{
//**SHOULD i do object...
//Scanner input;
//Formatter output;
Object obj;
ObjectOutputStream output;
ObjectInputStream input;
Socket connection;
ServerSocket serverSocket;
int clientNo;
//variables for calculation
//variables for calculation
double price;
double totalCharge;
public HandleAClient(Socket connection, int clientNo, double totalCharge)
{
this.connection = connection;
this.clientNo = clientNo;
this.totalCharge = totalCharge;
}
public void run()
{
//ArrayList<Computer> cList = new ArrayList<Computer>();
//connection = serverSocket.accept();
/*while(input.hasNext())
{
//variable = input.next....
//print out calculation
price = input.nextDouble();
System.out.println("Price received from client:\t"+clientNo+"is"+price);
//DO CALCULATION, STORE IT
for(Computer c: cList)//**TRYING a for loop
{
totalCharge = ((Computer)c).getPrice() * ((Computer)c).getQuantity();
output.format("%.2f\n", totalCharge);
//output.flush();
}
//}*/
System.out.println("\nTotal Charge\t"+totalCharge);
System.out.println("\nThread"+"\t"+clientNo+"\t"+"ended");
}
}
【问题讨论】:
-
为什么 System.in 上有 4 个扫描仪对象?
-
我跳过了行,我通过在 while 循环结束时执行scanner.nextLine() 解决了这个问题。谢谢
标签: java multithreading loops