小编bim*_*wla的帖子

可运行的接口示例

public class CreateThreadRunnableExample implements Runnable {

    public void run() {

        for (int i = 0; i < 5; i++) {
            System.out.println("Child Thread : " + i);

            try {
                Thread.sleep(50);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }

        System.out.println("Child thread finished!");
    }

    public static void main(String[] args) {

        Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");

        t.start();

        for (int i = 0; i < 5; i++) {

            System.out.println("Main thread : " + i);

            try {
                Thread.sleep(100); …
Run Code Online (Sandbox Code Playgroud)

java multithreading

11
推荐指数
1
解决办法
6万
查看次数

最后用java中的try方法

类DaemonThread扩展Thread {

public void run() {
    System.out.println("Entering run method");

    try {
        System.out.println("In run Method: currentThread() is"
            + Thread.currentThread());

        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException x) {
                System.out.println("hi");
            }

            // System.out.println("In run method: woke up again");

            finally {
                System.out.println("Leaving run1 Method");
            }
        }
    } finally {
        System.out.println("Leaving run Method");
    }

}

public static void main(String[] args) {
    System.out.println("Entering main Method");

    DaemonThread t = new DaemonThread();
    t.setDaemon(true);
    t.start();

    try {
        Thread.sleep(900);
    } catch (InterruptedException x) {}

    System.out.println("Leaving main method"); …
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
1148
查看次数

标签 统计

java ×2

multithreading ×1