pharmdock并行运算的java实现

上一篇用python实现的,用到map函数比较方便,用java实现起来可就没那么简单了,需要考虑线程共享数据问题、同步问题


class RunCmd implements Runnable
{
	int index =0;//全局变量,线程共享数据,否则每个线程都会执行一次同一条命令
	List<File> molsList = new ArrayList<File>();
	public RunCmd()
	{
		File f = new File("/map/ph");
		getFileList(f);
	}
	private void getFileList(File f)
	{
		File[] filePaths = f.listFiles();
		for (File mol : filePaths)
		{
			if (mol.getName().endsWith("_omega2.mol2"))

                        {

                                molsList.add(mol);

                        }	
		
		}
	}


	public void run() 
	{
		File file = null;
		
		while(index < molsList.size())
		{
			file=molsList.get(index);
			index++;
			System.out.println(Thread.currentThread().getName());	
			String cmd = "···";
			System.out.println(cmd);
			try
			{
				Process p = Runtime.getRuntime().exec(cmd);
				p.waitFor();//在cmd执行时其他线程需要等待到其执行完毕,否则会将任务全部提交到后台
			}
			catch(IOException e){}
			catch(InterruptedException i){}

		}

/*		该方法会有线程间通信问题
		Iterator it = list.iterator();
		while(it.hasNext())
		{	
			String cmd = "···";
			System.out.println(cmd);
			try
			{
				Runtime.getRuntime().exec(cmd);
			}
			catch(IOException e){}

		}
*/
	}

}

class ParRun
{
	public static void main(String[] args)
	{
		RunCmd r = new RunCmd();
		
		Thread t0 = new Thread(r);
		Thread t1 = new Thread(r);
		Thread t2 = new Thread(r);
		Thread t3 = new Thread(r);
		Thread t4 = new Thread(r);
		Thread t5 = new Thread(r);

		t0.start();
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		
	
	}
}

现在暂未考虑同步问题,以后改进

参考的文章:

http://bbs.csdn.net/topics/390773319

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值