开发者

Converting sequential Java to concurrent java code

开发者 https://www.devze.com 2022-12-21 17:20 出处:网络
I am planning to do a project in Java in which I want to transform asequential Java code to a concurrent Java code and measure the efficiency .Any suggestions on which free source java application wou

I am planning to do a project in Java in which I want to transform a sequential Java code to a concurrent Java code and measure the efficiency .Any suggestions on which free source java application would be best for开发者_如何学编程 my project.Any other suggestions on how to go about it?

Thanks in advance Deed


I think you can use Java Concurrency Framework, which comes with JDK. http://java.sun.com/j2se/1.5.0/docs/guide/concurrency/


The issue is whether the tasks that you are doing with your code can be run concurrently or not. If your code has been written as sequential, then you first need to identify what tasks can be run concurrently, what are the interdependencies between those tasks, whether they need to cooperate or whether they are independent. If they need to cooperate, you will need to have shared objects, locking etc. You need to decide whether you should use thread pools or create your own threads every time for every new task. Most of these would be very specific to your situation, so there can't be a generic answer. Once you have figured all that out check the java concurrency documentation on how to implement.


Here is the simplest solution.

Runnable runnableObject = new Runnable() {
   public void run() {
      try {
      ... call your original logic ...
      } catch (Exception ex) {
      ... Exception handling ...
      } finally {
      ... final action ...
      }
  }
}

ArrayList listThreads = new ArrayList();
for (int i=0; i<CONCURRENT_THREADS_SIZE; i++) {
   Thread threadObject = new Thread(runnableObject, "mythread-" + i);
   threadObject.start();
   listThreads.add(threadObject);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号