开发者

How to Send the Current Thread to Sleep

开发者 https://www.devze.com 2023-01-20 09:58 出处:网络
Thread thred = new Thread(); thred.run(); public void run() { while (true) { try { Thread.sleep(500)开发者_C百科;
Thread thred = new Thread();
        thred.run();


    public void run() {
        while (true)
        {

       try {
                Thread.sleep(500)开发者_C百科;
                Toast toast = Toast.makeText(getApplicationContext(), "Sleep Over", 100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Toast toast = Toast.makeText(getApplicationContext(), "Sleep NOT Over", 100);
            }
        }

This code don't work


Thread thred = new Thread()
{ 


    public void run() {
        while (true)
        {

         try {
                sleep(500);
                Toast toast = Toast.makeText(getApplicationContext(), "Sleep Over", 100);
             } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Toast toast = Toast.makeText(getApplicationContext(), "Sleep NOT Over", 100);
             }
        }
    }
};
thred.start();

You need to call the sleep method of the current thread Thread.sleep will call the static sleep method. You need to make sure you are overriding the run method of the thread class with your own run method and then calling thread.start not run.

0

精彩评论

暂无评论...
验证码 换一张
取 消