The problem is not because i=(i+1)%2 is never executed. You can verify this by adding a print statement after the i=(i+1)%2 like
System.out.println("i="+i);The problem is because there's a delay in else block, which makes female be overwritten right after the assignment. The sequence is:
male-----------> delay------------->female-->male ... So male has much long life than female. If you move the delay out of the else block, you should be able to see all the handsome males and beautiful femals. :-)Suggestion: use synchronized(..) for the thread programming.
System.out.println("i="+i);The problem is because there's a delay in else block, which makes female be overwritten right after the assignment. The sequence is:
male-----------> delay------------->female-->male ... So male has much long life than female. If you move the delay out of the else block, you should be able to see all the handsome males and beautiful femals. :-)Suggestion: use synchronized(..) for the thread programming.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货