标签:java
Thread():创建一个线程对象 |
Thread(Runnable target):创建一个带Runnabke对象参数的线程对象 |
Thread(Runnable target, String name):创建一个带Runnable对象参数的线程对象 |
Thread(String name):创建一个线程对象,并指定线程的名称
Allocates a new
Thread object. |
//主线程 public class DemoThread { public static void main(String arg[]) { TestThread thread = new TestThread(); //创建一个Thread子类对象 thread.start(); //启动该子线程 while(true) { System.out.println("main thread is running."); } } } //子线程的类 class TestThread extends Thread { public void run() { while(true) { System.out.println(Thread.currentThread().getName()+" is running."); } } }
//主线程 public class RunnableTest { public static void main(String args[]) { TestRunnable runnable = new TestRunnable(); //创建一个类对象,该类继承于Runnable接口 Thread thread=new Thread(runnable); //创建一个带Runnable对象参数的线程 thread.start(); //启动该线程 while(true) { System.out.println("main thread is running."); } } } //子线程 class TestRunnable implements Runnable { @Override public void run() { while(true) { System.out.println(Thread.currentThread().getName()+" is running."); } } }
//主线程 public class SaleTickets { public static void main(String[] arg) { new Resource().start();//启动线程1 new Resource().start();//启动线程2 new Resource().start();//启动线程3 new Resource().start();//启动线程4 } } //唯一资源 class Resource extends Thread { private int tickets=100; //100张票 @Override public void run() { //线程每运行一次run方法,票数减1 while(tickets>0) { System.out.println(Thread.currentThread().getName()+" is saling tickets "+tickets--); } } }
//主线程 public class SaleTickets { public static void main(String[] arg) { Resource r = new Resource(); //实例化一个Runnbale子类对象,代表一个资源 /*依次创建4个子线程*/ new Thread(r).start(); new Thread(r).start(); new Thread(r).start(); new Thread(r).start(); } } //唯一资源 class Resource implements Runnable { private int tickets=100; //100张票 @Override public void run() { //线程每运行一次run方法,票数减1 while(tickets>0) { System.out.println(Thread.currentThread().getName()+" is saling tickets "+tickets--); } } }
//主线程 public class RunnableTest { public static void main(String[] args) { int i=10; TestRunnable runnable = new TestRunnable(); //创建一个类对象,该类继承于Runnable接口 Thread thread=new Thread(runnable); //创建一个带Runnable对象参数的线程 thread.setDaemon(true); thread.start(); //启动该线程 while((--i>0)) { System.out.println("main thread is running."); } } } //子线程 class TestRunnable implements Runnable { @Override public void run() { while(true) { System.out.println(Thread.currentThread().getName()+" is running."); } } }
public class joinThead { public static void main(String[] args) { testThread t = new testThread(); //创建一个Runnable子类对象 Thread thread=new Thread(t); //创建一个线程 thread.start(); //启动线程 int i=100; while(i>0) { if(i==50) { try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("main thread:"+i--); } } } class testThread implements Runnable { private int i=100; public void run() { while(i>0) { i--; System.out.println(Thread.currentThread().getName()+": "+i ); } } }
标签:java
原文地址:http://blog.csdn.net/u012637501/article/details/43124591