我有一个关于本地和远程方法如何在Java RMI中协作的问题。 这是理想的情况:
localClass.setValue(server.getValue());
localClass.setValue(..)
等待来自服务器的返回值,还是必须在本地使用某种同步来确保它?
如果服务器需要5秒钟来执行getValue()
会发生什么?
答案 0 :(得分:0)
客户端等待5秒,如果您想设置超时异常,则必须按照 this 问题中的建议自行完成
答案 1 :(得分:0)
不完全。
在调用方法之前,从左到右计算参数。
在localClass.setValue()
返回的参数值可用之前,甚至不会调用server.getValue()
。因此客户端只要等待,然后就会调用localClass.setValue()
。执行等待的setValue()
不是server.getValue()
,而是#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include "/Boost/boost_1_65_1/boost_1_65_1/boost/graph/adjacency_list.hpp"
#include "/Boost/boost_1_65_1/boost_1_65_1/boost/graph/graph_utility.hpp"
#include "/Boost/boost_1_65_1/boost_1_65_1/boost/graph/visitors.hpp"
#include "/Boost/boost_1_65_1/boost_1_65_1/boost/graph/breadth_first_search.hpp"
#include "/Boost/boost_1_65_1/boost_1_65_1/boost/graph/dijkstra_shortest_paths.hpp"
#include "/Boost/boost_1_65_1/boost_1_65_1/boost/graph/prim_minimum_spanning_tree.hpp"
#include "/Boost/boost_1_65_1/boost_1_65_1/boost/property_map/property_map.hpp"
#include "/Boost/boost_1_65_1/boost_1_65_1/boost/graph/astar_search.hpp"
using namespace boost;
using namespace std;
typedef boost::adjacency_list_traits<vecS, vecS, undirectedS> GraphTraits;
typedef GraphTraits::vertex_descriptor Vertex;
struct VertexProperty {
string name;
Vertex predecessor;
double distance;
default_color_type color;
VertexProperty(const string& aName = "") : name(aName) { };
};
struct EdgeProperty {
double weight; // distance to travel along this edge.
EdgeProperty(double aWeight = 0.0) : weight(aWeight) { };
};
typedef adjacency_list<vecS, vecS, undirectedS, VertexProperty, EdgeProperty> Graph;
Graph g;
struct do_nothing_dijkstra_visitor : default_dijkstra_visitor{};
int main() {
string tempName1, tempName2, tempString, data2;
int weight;
string inputFile;
int choice;
Vertex cur_v, start_v, goal_v;
map<string, Vertex> name2v, name1v;
double totalDist, tempDist;
int numVert = 0;
while (1) {
cout << "\n-------------------------" << endl;
cout << " Graph Implementation";
cout << "\n-------------------------" << endl << endl;
cout << "1. Insert File into Graph" << endl;
cout << "2. Shortest Paths in a Network" << endl;
cout << "3. Minimum Spanning Tree" << endl;
cout << "4. Travelling Salesman" << endl;
cout << "5. Exit" << endl << endl;
cout << "Selection: ";
cin >> choice;
cout << endl;
switch (choice) {
case 1:
{
cout << "Please enter name of file you would like to input: ";
cin >> inputFile;
cout << endl;
ifstream fin;
fin.open(inputFile);
if (!fin) {
cerr << "Cant open data file, goodbye..." << endl;
system("Pause");
return EXIT_FAILURE;
}
else {
cout << "File loaded." << endl << endl;
}
//build graph based on file loaded
getline(fin, tempString);
getline(fin, tempString);
stringstream tempSS(tempString);
while (getline(tempSS, tempName1, ',')) {
name2v[tempName1] = add_vertex(VertexProperty(tempName1), g);
numVert++;
}
getline(fin, tempString);
while (getline(fin, tempString)) {
tempString.erase(tempString.begin(), tempString.begin() + tempString.find('(') + 1);
tempString.erase(tempString.begin() + tempString.find(')'), tempString.end());
stringstream temp_ss(tempString);
getline(temp_ss, tempName1, ',');
getline(temp_ss, tempName2, ',');
temp_ss >> weight;
add_edge(name2v[tempName1], name2v[tempName2], EdgeProperty(weight), g);
}
name1v = name2v;
cout << "Graph Created" << endl;
print_graph(g, get(&VertexProperty::name, g));
break;
}
case 2:
{
totalDist = 0;
cout << endl << "Please enter the location name to start from: ";
cin >> tempName1;
transform(tempName1.begin(), tempName1.end(), tempName1.begin(), ::toupper);
cout << endl << "Please enter the location name for the destination: ";
cin >> tempName2;
transform(tempName2.begin(), tempName2.end(), tempName2.begin(), ::toupper);
start_v = name2v[tempName1];
goal_v = name2v[tempName2];
dijkstra_shortest_paths(
g, goal_v,
get(&VertexProperty::predecessor, g),
get(&VertexProperty::distance, g),
get(&EdgeProperty::weight, g),
identity_property_map(),
less<double>(),
plus<double>(),
numeric_limits<double>::infinity(),
0.0,
do_nothing_dijkstra_visitor(),
get(&VertexProperty::color, g));
cout << endl;
cout << "Shortest Path From " << tempName1 << " to " << tempName2 << ": "<< endl;
cur_v = start_v;
while (cur_v != goal_v) {
cout << "(" << g[cur_v].name << ", ";
totalDist += g[cur_v].distance;
tempDist = g[cur_v].distance;
cur_v = g[cur_v].predecessor;
cout << g[cur_v].name << ")" << endl;
totalDist -= g[cur_v].distance;
};
cout << "Total Weight: " << totalDist << endl;
name2v = name1v;
break;
}
case 3:
{
totalDist = 0;
Graph::vertex_descriptor start_w;
cout << "Please enter the Vertex you would like to start at: ";
{
string startName;
cin >> startName;
transform(startName.begin(), startName.end(), startName.begin(), ::toupper);
start_w = name2v.at(startName);
}
prim_minimum_spanning_tree(g, start_w,
get(&VertexProperty::predecessor, g),
get(&VertexProperty::distance, g),
get(&EdgeProperty::weight, g),
identity_property_map(),
do_nothing_dijkstra_visitor());
cout << endl;
cout << "Minimum Spanning Tree: " << endl;
for (auto vd : make_iterator_range(vertices(g))) {
auto p = g[vd].predecessor;
if (g[vd].name != g[p].name) {
cout << "(" << g[vd].name << ", " << g[p].name << ")" << endl;
totalDist += g[vd].distance;
}
}
cout << "Total Weight: " << totalDist;
name2v = name1v;
break;
}
case 4:
Graph::vertex_descriptor start_x;
cout << "Please enter the Vertex you would like to start at: ";
{
string startName;
cin >> startName;
transform(startName.begin(), startName.end(), startName.begin(), ::toupper);
start_x = name2v.at(startName);
}
break;
case 5:
{
cout << "Goodbye" << endl;
exit(1);
break;
}
default:
cout << "Make a correct choice" << endl;
}
}
system("Pause");
return EXIT_SUCCESS;
}
的存根调用。
你自己没有任何关于它的事情。