02 创建线程方式

  • 继承Thread
/** extends Thread 非实现变量共享 */
public class MyThread extends Thread {
    private int count = 3;

    public MyThread(String threadName) {
        super();
        this.setName(threadName);
    }

    @Override
    public void run() {
        while (count > 0) {
            count--;
            System.out.println("thread " + Thread.currentThread().getName()
                    + " count = " + count);
        }
    }

    /**
     * 使用extends Thread 创建3个线程(即创建3个MyThread实例对象)
     * 每个线程都有各自的count变量, 自己减少自己的count值, 因此变量不共享。
     */
    public static void init() {
        new MyThread("A").start();
        new MyThread("B").start();
        new MyThread("C").start();
    }

    public static void main(String[] args) {
        init();
    }
    /** 打印结果
     * thread B count = 2
     * thread C count = 2
     * thread C count = 1
     * thread C count = 0
     * thread B count = 1
     * thread B count = 0
     * thread A count = 2
     * thread A count = 1
     * thread A count = 0
     */
}
/** extends Thread 实现变量共享 */
public class MyThread extends Thread {
    private static Integer tickets = 5;
    private static String str = new String("Sell Tickets"); // 必须加static

    public MyThread(String threadName) {
        super();
        this.setName(threadName);
    }

    @Override
    public void run() {
        while (true) {
            synchronized (str) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()
                            + " 正在出售第 " + (tickets--) + " 张");
                } else {
                    break;
                }
            }
        }
    }

    public static void init() {
        new MyThread("A").start();
        new MyThread("B").start();
        new MyThread("C").start();
    }

    public static void main(String[] args) {
        init();
    }
    /** 打印结果
     * thread A tickets = 5
     * thread C tickets = 4
     * thread B tickets = 3
     * thread B tickets = 2
     * thread B tickets = 1
     */
}
  • 实现Runnable接口
// 运行线程的target是同一对象, 变量共享
public class MyThread implements Runnable {
    int i = 5;
    @Override
    public void run() {
        while(true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + this.i);
        }
    }

    public static void init() {
        MyThread2 target = new MyThread2();
        new Thread(target, "A").start();
        new Thread(target, "B").start();
        new Thread(target, "C").start();
    }

    public static void main(String[] args) {
        init();
    }
}
  • 匿名内部类启动线程
// Thread对象
public static void main(String[] args) {
    new Thread() {
        public void run() {
            for (int x = 0; x < 10; x++) {
                System.out.println(
                        Thread.currentThread().getName() + " x = " + x);
                }
            }
        }.start();
    }
// 实现runnable借口,创建多线程并启动
public static void main(String[] args) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int x = 0; x < 10; x++) {
                System.out.println(
                        Thread.currentThread().getName() + " x = " + x);
            }
        }
    }) {
    }.start();
}    
public static void main(String[] args) {
    new Thread(
            new Runnable() {
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("runnable :"
                                + Thread.currentThread().getName());
                    }
                }
            }
    ) {
        // 子类覆盖父类的run方法, 所以会调用自己的run方法, 若子类中没有run方法则回去找父类的run
        public void run() {
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(
                        "thread :" + Thread.currentThread().getName());
            }
        }
    }.start();
}
// 打印结果:thread :Thread-0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值