java.lang.InterruptedException: sleep interrupted异常

使用单元测试,创建线程池,然后启动一个线程,在线程中sleep(5*1000),这是回报

java.lang.InterruptedException: sleep interrupted异常,原因是因为单元测试启动的主线程很快就结束了,而子线程确sleep5秒,使得主线程强行打断子线程的sleep,因此抛出异常,解决办法是可以在单元测试的最后加上sleep(10*1000),目的是不让主线程在子线程前结束。

 

 

 @Test

    public void getUserPwd(){

        Test1Thread thread1 = new Test1Thread(userService,

                "ptstest3", MD5Encrypt.md5("12345678"));

        taskExecutor.execute(thread1);

 

try {

            Thread.sleep(10*1000);

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

 

 

 

 

public class Test1Thread implements Runnable {

 

    public UserService userService;

    public String loginId;

    public String pwd;

 

    public Test1Thread(UserService userService,String loginId,String pwd){

        this.pwd=pwd;

        this.loginId=loginId;

        this.userService=userService;

    }

 

    /* (non-Javadoc)

     * @see java.lang.Runnable#run()

     */

    @Override

    public void run() {

        // TODO Auto-generated method stub

        try {

            userService.updateUserPassword(loginId, pwd);

            Thread.sleep(5*1000);

        } catch (InterruptedException e) {

            e.printStackTrace();

            System.out.println("error--------");

        }

 

    }

 

}

 

 

你可能感兴趣的:(exception)