=三个线程顺序执行
package lee.hao.review;
import java.io.IOException;
public class Test3 {
public static void main(String arg[]) throws IOException {
final Test obj = new Test();
new Thread() {
public void run() {
obj.m1();
}
}.start();
new Thread() {
public void run() {
obj.m2();
}
}.start();
new Thread() {
public void run() {
obj.m3();
}
}.start();
}
}
class Test {
static int count;
volatile int target = 1;
synchronized void m1() {
for (int i = 0; i < 10; i++) {
while (target == 2 || target == 3) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("m1() =" + i);
target = 2;
notifyAll();
}
}
synchronized void m2() {
for (int i = 0; i < 10; i++) {
while (target == 1 || target == 3) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("m2() =" + i);
target = 3;
notifyAll();
}
}
synchronized void m3() {
for (int i = 0; i < 10; i++) {
while (target == 1 || target == 2) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("m3() =" + i);
target = 1;
notifyAll();
}
}
}
迅雷笔试最后一道题,三个线程顺序执行。。。哎
数组交叉移动问题
题目:输入数组:{a1,a2,…,an,b1,b2,…,bn}, 在O(n)的时间,O(1)的空间将这个数组的顺序变为{a1,b1,a2,b2,a3,b3,…,an,bn}, 且不需要移动,通过交换完成,只需一个交换空间。
解答:从结果入手,结果数组的中垂线两边分别a数组的一半和b数组的一半的混合,继续将子数组以中垂线划分下去,可以看到类似的规律,因此,可以使用类似的分治算法实现。