Java线程,消费者不想结束
为什么我的线程(Runnable)在到达块的末尾时不退出run()方法 代码如下:Java线程,消费者不想结束,java,multithreading,consumer,producer,Java,Multithreading,Consumer,Producer,为什么我的线程(Runnable)在到达块的末尾时不退出run()方法 代码如下: //1 Entry point for thread with run method public void run() { System.out.println("Hashmaker():/run(). " + threadName + " Running " ); try { String original = in; MessageDigest md = M
//1 Entry point for thread with run method
public void run() {
System.out.println("Hashmaker():/run(). " + threadName + " Running " );
try {
String original = in;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
System.out.println("Hashmaker:/run(). original: " + original);
System.out.println("Hashmaker:/run(). digested(hex):" + sb.toString());
//src http://www.avajava.com/tutorials/lessons/how-do-i-generate-an-md5-digest-for-a-string.html
}
// Let the thread sleep for a while.
//Thread.sleep(7);
System.out.println("Hashmaker:/run(). Thread: " + threadName + " recieved a clear password = " + in);
//Save the hash
hash=sb.toString();
}
/*catch (InterruptedException e) {
System.out.println("Hashmaker:/run(). Thread " + threadName + " interrupted.");
} */
catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hashmaker:/run(). Thread " + threadName + " exiting.");
}
理想情况下,它应该正常结束,然后我的main()方法将开始其他内容:
public static void main(String[] args) throws Exception {
//Parse the args
if (args.length != 1) {
System.err.println("Main()/: String to MD5 digest should be first parameter\n");
System.out.println("Main()/: Require now a clear password with 4 char :");
sc = new Scanner(System.in);
in = sc.nextLine();
force = 2;
}
else{
in = args[0];
force = Integer.parseInt(args[1]);
}
//Set the count of Breakers
count=force;
//Make the hash
Hashmaker hm = new Hashmaker(in,"Hashmaker");
hm.start();
System.out.println("Main()/: Managing the breakers will start !");
//Get the hash
hash=hm.getHash();
//Manage the Breakers
f = new File(count);
for(int i=0; i<count;i++){
Hashbreaker hb = new Hashbreaker(hash,"HashBreaker"+i);
f.enfiler(hb);
hb.start();
}
}
publicstaticvoidmain(字符串[]args)引发异常{
//解析args
如果(args.length!=1){
System.err.println(“Main()/:MD5摘要的字符串应该是第一个参数\n”);
System.out.println(“Main()/:现在需要一个带有4个字符的清晰密码:”;
sc=新扫描仪(系统英寸);
in=sc.nextLine();
力=2;
}
否则{
in=args[0];
force=Integer.parseInt(args[1]);
}
//设置断路器的计数
计数=力;
//做杂烩
Hashmaker hm=新的Hashmaker(在“Hashmaker”中);
hm.start();
System.out.println(“Main()/:管理断路器将启动!”);
//得到散列
hash=hm.getHash();
//管理断路器
f=新文件(计数);
对于(int i=0;我不明白为什么您需要一个专用线程来创建散列,为什么不让主线程来创建散列。您能澄清一下吗?尤其是在这里,您的“生产者”中没有任何while循环因此,它将只生成一个Hashbreaker。您可以发布Hashbreaker的代码吗?它是实现了可运行的
还是扩展了线程
?还有您的Hashmaker
,它实现了哪个类?因为如果它再次是可运行的,这意味着当您调用hm.getHash()时;
,值为空,因此您的消费者会收到一个空值“它应该像一个类线程一样正常结束,然后我的Main应该开始其他东西”。您的主线程不会等待Hashmaker
完成(除非getHash()
块),如前所述,如果希望主线程等待Hashmaker
完成其工作,则不需要单独的线程。@Kayaman好的,现在我正在编写技术报告,不再在代码中。但我将尝试删除“getHash()”看看线程是否在块结束后死亡。@AntJavaDev我可以,但我认为这对我的问题没有用。如果hashmaker没有结束,hashbreaker甚至不会被调用一次。