标签:
package com.csdhsm.concurrent; /** * @Title: ThreadDemo.java * @Package: com.csdhsm.concurrent * @Description 继承Thread测试 * @author Han * @date 2016-4-18 下午3:45:30 * @version V1.0 */ public class ThreadDemo extends Thread { private int ticket = 3; private int id; public ThreadDemo(int id) { this.id = id; } @Override public void run() { while(ticket > 0){ System.out.println(this.id + " --> " + ticket--); } } public static void main(String[] args) { new ThreadDemo(1).start(); new ThreadDemo(2).start(); new ThreadDemo(3).start(); } }
package com.csdhsm.concurrent; /** * @Title: RunnableDemo.java * @Package: com.csdhsm.concurrent * @Description 实现Runnale接口 * @author Han * @date 2016-4-18 下午3:47:40 * @version V1.0 */ public class RunnableDemo implements Runnable{ private int ticket = 5; private int id; public RunnableDemo(int id) { this.id = id; } @Override public void run() { while(ticket > 0){ System.out.println(this.id + " --> " + ticket--); } } public static void main(String[] args) { RunnableDemo demo = new RunnableDemo(1); new Thread(demo).start(); new Thread(demo).start(); new Thread(demo).start(); } }
Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)
标签:
原文地址:http://www.cnblogs.com/a294098789/p/5402175.html