疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

用于执行密集型任务的java程序


 

  通过Windows Azure,你可以使用一个虚拟机来处理计算密集型任务。前面给大家介绍了云平台上创建一个虚拟机和云平台上创建一个服务总线命名空间,本文将给您介绍一下如何创建一个执行计算密集型任务的Java应用程序。

  1. 在你的开发机器(并不一定是你所创建的虚拟机)上,为Java下载Windows Azure SDK.

  2. 在本小节的末尾创建一个Java控制台应用程序使用的示例代码。在本教程中,我们将使用TSPSolver.Java作为Java文件的名字。修改your_service_bus_namespace、 your_service_bus_owner、 以及 your_service_bus_key 等占位符,分别使用您的服务总线命名空间、 默认发行人和默认密钥值。

  3. 编码后,导出应用程序到一个可运行的Java归档(JAR),并把所需的库到打包已生成的JAR.在本教程中,我们将使用TSPSolver.jar作为生成的JAR名称。

  // TSPSolver.Java

  import com.microsoft.WindowsAzure.services.core.Configuration;

  import com.microsoft.WindowsAzure.services.core.ServiceException;

  import com.microsoft.WindowsAzure.services.serviceBus.*;

  import com.microsoft.WindowsAzure.services.serviceBus.models.*;

  import Java.io.*;

  import Java.text.DateFormat;

  import Java.text.SimpleDateFormat;

  import Java.util.ArrayList;

  import Java.util.Date;

  import Java.util.List;

  public class TSPSolver {

  // Value specifying how often to provide an update to the console.

  private static long loopCheck = 100000000;

  private static long nTimes = 0, nLoops=0;

  private static double[][] distances;

  private static String[] cityNames;

  private static int[] bestOrder;

  private static double minDistance;

  private static ServiceBusContract service;

  private static void buildDistances(String fileLocation, int numCities) throws Exception{

  try{

  BufferedReader file = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(new File(fileLocation)))));

  double[][] cityLocs = new double[numCities][2];

  for (int i = 0; i

  String[] line = file.readLine()。split(“, ”);

  cityNames[i] = line[0];

  cityLocs[i][0] = Double.parseDouble(line[1]);

  cityLocs[i][1] = Double.parseDouble(line[2]);

  }

  for (int i = 0; i

  for (int j = i; j

  distances[i][j] = Math.hypot(Math.abs(cityLocs[i][0] - cityLocs[j][0]), Math.abs(cityLocs[i][1] - cityLocs[j][1]));

  distances[j][i] = distances[i][j];

  }

  }

  } catch (Exception e){

  throw e;

  }

  }

  private static void permutation(List startCities, double distSoFar, List restCities) throws Exception {

  try

  {

  nTimes++;

  if (nTimes == loopCheck)

  {

  nLoops++;

  nTimes = 0;

  DateFormat dateFormat = new SimpleDateFormat(“MM/dd/yyyy HH:mm:ss”);

  Date date = new Date();

  System.out.print(“Current time is ” + dateFormat.format(date) + “. ”);

  System.out.println( “Completed ” + nLoops + “ iterations of size of ” + loopCheck + “.”);

  }

  if ((restCities.size() == 1) && ((minDistance == -1) || (distSoFar + distances[restCities.get(0)][startCities.get(0)] + distances[restCities.get(0)][startCities.get(startCities.size()-1)] < minDistance))){

  startCities.add(restCities.get(0));

  newBestDistance(startCities, distSoFar + distances[restCities.get(0)][startCities.get(0)] + distances[restCities.get(0)][startCities.get(startCities.size()-2)]);

  startCities.remove(startCities.size()-1);

  }

  else{

  for (int i=0; i

  startCities.add(restCities.get(0));

  restCities.remove(0);

  permutation(startCities, distSoFar + distances[startCities.get(startCities.size()-1)][startCities.get(startCities.size()-2)],restCities);

  restCities.add(startCities.get(startCities.size()-1));

  startCities.remove(startCities.size()-1);

  }

  }

  }

  catch (Exception e)

  {

  throw e;

  }

  }

  private static void newBestDistance(List cities, double distance) throws ServiceException, Exception {

  try

  {

  minDistance = distance;

  String cityList = “Shortest distance is ”+minDistance+“, with route: ”;

  for (int i = 0; i

  bestOrder[i] = cities.get(i);

  cityList += cityNames[bestOrder[i]];

  if (i != bestOrder.length -1)

  cityList += “, ”;

  }

  System.out.println(cityList);

  service.sendQueueMessage(“TSPQueue”, new BrokeredMessage(cityList));

  }

  catch (ServiceException se)

  {

  throw se;

  }

  catch (Exception e)

  {

  throw e;

  }

  }

  public static void main(String args[]){

  try {

  Configuration config = ServiceBusConfiguration.configureWithWrapAuthentication(

  “your_service_bus_namespace”, “your_service_bus_owner”, “your_service_bus_key”);

  service = ServiceBusService.create(config);

  int numCities = 10; // Use as the default, if no value is specified at command line.

  if (args.length != 0)

  {

  if (args[0].toLowerCase()。compareTo(“createqueue”)==0)

  {

  // No processing to occur other than creating the queue.

  QueueInfo queueInfo = new QueueInfo(“TSPQueue”);

  service.createQueue(queueInfo);

  System.out.println(“Queue named TSPQueue was created.”);

  System.exit(0);

  }

  if (args[0].toLowerCase()。compareTo(“deletequeue”)==0)

  {

  // No processing to occur other than deleting the queue.

  service.deleteQueue(“TSPQueue”);

  System.out.println(“Queue named TSPQueue was deleted.”);

  System.exit(0);

  }

  // Neither creating or deleting a queue.

  // Assume the value passed in is the number of cities to solve.

  numCities = Integer.valueOf(args[0]);

  }

  System.out.println(“Running for ” + numCities + “ cities.”);

  List startCities = new ArrayList();

  List restCities = new ArrayList();

  startCities.add(0);

  for(int i = 1; i

  restCities.add(i);

  distances = new double[numCities][numCities];

  cityNames = new String[numCities];

  buildDistances(“c:TSPcities.txt”, numCities);

  minDistance = -1;

  bestOrder = new int[numCities];

  permutation(startCities, 0, restCities);

  System.out.println(“Final solution found!”);

  service.sendQueueMessage(“TSPQueue”, new BrokeredMessage(“Complete”));

  }

  catch (ServiceException se)

  {

  System.out.println(se.getMessage());

  se.printStackTrace();

  System.exit(-1);

  }

  catch (Exception e)

  {

  System.out.println(e.getMessage());

  e.printStackTrace();

  System.exit(-1);

  }

  }

  }