单元测试线程死亡
使用junit编写线程进行Sleep操作时,若将Sleep放入run方法中,对test()进行单元测试,则线程会在Sleep处结束死亡
public class ThreadTest implements Runnable{
@Override
public void run() {
try {
System.out.println("子伦准备运行");
System.out.println("子伦运行起来了!!!");
for(int i=0;i<5;i++) {
Thread.sleep(1000);
System.out.println("子伦睡醒了!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void test() {
ThreadTest t=new ThreadTest();
Thread th=new Thread(t);
th.start();
System.out.println(th.getName());
}
}
解决办法:1.使用main进行测试即可
public class ThreadTest implements Runnable{
@Override
public void run() {
try {
System.out.println("子伦准备运行");
System.out.println("子伦运行起来了!!!");
for(int i=0;i<5;i++) {
Thread.sleep(1000);
System.out.println("子伦睡醒了!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void test() {
ThreadTest t=new ThreadTest();
Thread th=new Thread(t);
th.start();
System.out.println(th.getName());
}
public static void main(String[] args) {
ThreadTest t=new ThreadTest();
Thread th=new Thread(t);
th.start();
th.setName("子伦进程");
System.out.println(th.getName());
}
}
2.在单元测试的方法内进行Sleep
@Override
public void run() {
try {
System.out.println("子伦准备运行");
System.out.println("子伦运行起来了!!!");
for(int i=0;i<5;i++) {
System.out.println("子伦睡醒了!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void test() {
ThreadTest t=new ThreadTest();
Thread th=new Thread(t);
th.start();
try {
th.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(th.getName());
}
Arrays常用方法
- Arrays.sort();对数组进行从小到大的排序
- Arrays.toString();对数组进行String类型转换
- Arrays.copyof(数组,扩容后长度);对数组进行扩容,返回值为一个新的数组