Java Thread实验

public class threadtest1 {

    public static void main(String[] args) throws InterruptedException {

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("thread 1:" + i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("thread 2:" + i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("thread 3:" + i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        thread1.start();

        thread1.join();

        thread2.start();

        thread2.join();

        thread3.start();

        System.out.println("Hello World");
    }
}
输出:
thread 1:0
thread 1:1
thread 1:2
thread 1:3
thread 1:4
thread 2:0
thread 2:1
thread 2:2
thread 2:3
thread 2:4
Hello World
thread 3:0
thread 3:1
thread 3:2
thread 3:3
thread 3:4
public class threadtest2 {

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                // 线程1
                Thread thread1 = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 5; i++) {
                            System.out.println("thread 1:" + i);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });

                // 线程2
                Thread thread2 = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 5; i++) {
                            System.out.println("thread 2:" + i);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });

                thread1.start();

                try {
                    thread1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                thread2.start();
            }
        });

        thread.start();

        System.out.println("Hello World");
    }
}
输出:
Hello World
thread 1:0
thread 1:1
thread 1:2
thread 1:3
thread 1:4
thread 2:0
thread 2:1
thread 2:2
thread 2:3
thread 2:4
public class threadtest3 {
    public static void main(String[] args) throws InterruptedException {
        for (int j = 0; j < 3; j++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {

                    // 线程1
                    Thread thread1 = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < 5; i++) {
                                System.out.println("thread 1:" + i);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });

                    // 线程2
                    Thread thread2 = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < 5; i++) {
                                System.out.println("thread 2:" + i);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });

                    thread1.start();

                    try {
                        thread1.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    thread2.start();
                }
            });

            thread.start();
        }

        System.out.println("Hello World");
    }
}
输出:
Hello World
thread 1:0
thread 1:0
thread 1:0
thread 1:1
thread 1:1
thread 1:1
thread 1:2
thread 1:2
thread 1:2
thread 1:3
thread 1:3
thread 1:3
thread 1:4
thread 1:4
thread 1:4
thread 2:0
thread 2:0
thread 2:0
thread 2:1
thread 2:1
thread 2:1
thread 2:2
thread 2:2
thread 2:2
thread 2:3
thread 2:3
thread 2:3
thread 2:4
thread 2:4
thread 2:4

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值