-2

我有一个实现可运行的java程序。在正文中,我有Thread animator = new Thread();thenanimator.start();问题是run()我的程序的方法没有执行。我错过了什么吗?

4

2 回答 2

2

正如你所说的实现可运行的java程序。

在你写的那个类(名字说动画师)身上

Thread animator = new Thread();
animator.start();

如果我没有错

通过可运行的类实例,我认为它会this在创建线程时

Thread animator = new Thread(this);
animator.start();
于 2012-04-29T05:44:36.510 回答
1

你可以这样试试

public class BackgroundActivity {

/**
 * Attempts to execute the user activity.
 * 
 * @return The thread on which the operations are executed.
 */
public Thread doWork() {
    final Runnable runnable = new Runnable() {
        public void run() {
            System.out.println("Background Task here");
        }
    };

    // run on background thread.
    return performOnBackgroundThread(runnable);
}

/**
 * Executes your requests on a separate thread.
 * 
 * @param runnable
 *            The runnable instance containing mOperations to be executed.
 */
private Thread performOnBackgroundThread(final Runnable runnable) {
    final Thread t = new Thread() {
        @Override
        public void run() {
            try {
                runnable.run();
            } finally {
            }
        }
    };
    t.start();
    return t;
}

}

最后是 main 方法中的 doWork() 方法

 /**
 * @param args
 */
public static void main(String[] args) {
    BackgroundActivity ba = new BackgroundActivity();
    Thread thread = ba.doWork();

    //You  can manages thread here
}

希望,它会帮助你。

于 2012-04-29T05:54:14.587 回答