标签:
NO 方法名称 类型 描述 1 public Thread(Runnable target) 构造 接收Runnable接口子类对象,实例化Thread对象 2 public Thread(Runnable target,String name) 构造 接收Runnable接口子类对象,实例化Thread对象,并设置线程名称 3 public Thread(String name) 构造 实例化Thread对象,并设置线程名称。 4 public static Thread currentThread() 普通 返回目前正在执行的线程。 5 public final String getName() 普通 返回线程的名称 6 public final int getPriority() 普通 返回线程的优先级 7 public boolean isInterrupted() 普通 判断目前线程是否被中断,如果是,返回true,否则返回false 8 public final boolean isActive() 普通 判断线程是否在活动,如果是返回true,否则返回false 9 public final void join() throws InterruptedException 普通 等待线程死亡 10 public final synchronized void join(long millis) throws InterruptedException 普通 等待millis毫秒后,线程死亡。 11 public void run() 普通 执行线程 12 public final void setName() 普通 设定线程名称 13 public final void setPriority(int newPriority) 普通 设定线程的优先级 14 public static void sleep(long millis) throws InterruptedException 普通 使目前正在执行的线程休眠millis毫秒 15 public void start() 普通 开始执行线程。 16 public static void yield() 普通 将目前正在执行的线程暂停一次,允许其他线程执行 17 public final void setDaemon(boolean on) 普通 将一个线程设置成后台运行 18 public final void setPriority(int newPriority) 普通 更改线程的优先级
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i<3;i++){ System.out.println(Thread.currentThread().getName() + "运行,i = " + i) ; // 取得当前线程的名字 } } }; public class ThreadNameDemo{ public static void main(String args[]){ MyThread mt = new MyThread() ; // 实例化Runnable子类对象 new Thread(mt).start() ; // 系统自动设置线程名称 new Thread(mt,"线程-A").start() ; // 手工设置线程名称 new Thread(mt,"线程-B").start() ; // 手工设置线程名称 new Thread(mt).start() ; // 系统自动设置线程名称 new Thread(mt).start() ; // 系统自动设置线程名称 } };
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i<3;i++){ System.out.println(Thread.currentThread().getName() + "运行,i = " + i) ; // 取得当前线程的名字 } } }; public class CurrentThreadDemo{ public static void main(String args[]){ MyThread mt = new MyThread() ; // 实例化Runnable子类对象 new Thread(mt,"线程").start() ; // 启动线程 mt.run() ; // 直接调用run()方法 } };
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i<3;i++){ System.out.println(Thread.currentThread().getName() + "运行,i = " + i) ; // 取得当前线程的名字 } } }; public class ThreadAliveDemo{ public static void main(String args[]){ MyThread mt = new MyThread() ; // 实例化Runnable子类对象 Thread t = new Thread(mt,"线程"); // 实例化Thread对象 System.out.println("线程开始执行之前 --> " + t.isAlive()) ; // 判断是否启动 t.start() ; // 启动线程 System.out.println("线程开始执行之后 --> " + t.isAlive()) ; // 判断是否启动 for(int i=0;i<3;i++){ System.out.println(" main运行 --> " + i) ; } // 以下的输出结果不确定 System.out.println("代码执行之后 --> " + t.isAlive()) ; // 判断是否启动 } };
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i<50;i++){ System.out.println(Thread.currentThread().getName() + "运行,i = " + i) ; // 取得当前线程的名字 } } }; public class ThreadJoinDemo{ public static void main(String args[]){ MyThread mt = new MyThread() ; // 实例化Runnable子类对象 Thread t = new Thread(mt,"线程"); // 实例化Thread对象 t.start() ; // 启动线程 for(int i=0;i<50;i++){ if(i>10){ try{ t.join() ; // 线程强制运行 }catch(InterruptedException e){} } System.out.println("Main线程运行 --> " + i) ; } } };
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i<50;i++){ try{ Thread.sleep(500) ; // 线程休眠 }catch(InterruptedException e){} System.out.println(Thread.currentThread().getName() + "运行,i = " + i) ; // 取得当前线程的名字 } } }; public class ThreadSleepDemo{ public static void main(String args[]){ MyThread mt = new MyThread() ; // 实例化Runnable子类对象 Thread t = new Thread(mt,"线程"); // 实例化Thread对象 t.start() ; // 启动线程 } };会发现每隔500毫秒,执行一次。
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 System.out.println("1、进入run()方法") ; try{ Thread.sleep(10000) ; // 线程休眠10秒 System.out.println("2、已经完成了休眠") ; }catch(InterruptedException e){ System.out.println("3、休眠被终止") ; return ; // 返回调用处 } System.out.println("4、run()方法正常结束") ; } }; public class ThreadInterruptDemo{ public static void main(String args[]){ MyThread mt = new MyThread() ; // 实例化Runnable子类对象 Thread t = new Thread(mt,"线程"); // 实例化Thread对象 t.start() ; // 启动线程 try{ Thread.sleep(2000) ; // 线程休眠2秒 }catch(InterruptedException e){ System.out.println("3、休眠被终止") ; } t.interrupt() ; // 中断线程执行 } };
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 while(true){ System.out.println(Thread.currentThread().getName() + "在运行。") ; } } }; public class ThreadDaemonDemo{ public static void main(String args[]){ MyThread mt = new MyThread() ; // 实例化Runnable子类对象 Thread t = new Thread(mt,"线程"); // 实例化Thread对象 t.setDaemon(true) ; // 此线程在后台运行 t.start() ; // 启动线程 } };会发现没有打印任何东西,如果设置为false的话则会一直打印,不会结束进程。
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i<5;i++){ try{ Thread.sleep(500) ; // 线程休眠 }catch(InterruptedException e){} System.out.println(Thread.currentThread().getName() + "运行,i = " + i) ; // 取得当前线程的名字 } } }; public class ThreadPriorityDemo{ public static void main(String args[]){ Thread t1 = new Thread(new MyThread(),"线程A") ; // 实例化线程对象 Thread t2 = new Thread(new MyThread(),"线程B") ; // 实例化线程对象 Thread t3 = new Thread(new MyThread(),"线程C") ; // 实例化线程对象 t1.setPriority(Thread.MIN_PRIORITY) ; // 优先级最低 t2.setPriority(Thread.MAX_PRIORITY) ; // 优先级最高 t3.setPriority(Thread.NORM_PRIORITY) ; // 优先级居中 t1.start() ; // 启动线程 t2.start() ; // 启动线程 t3.start() ; // 启动线程 } };
public class MainPriorityDemo{ public static void main(String args[]){ System.out.println("主方法的优先级:" + Thread.currentThread().getPriority()) ; // 取得主方法的优先级 System.out.println("MAX_PRIORITY = " + Thread.MAX_PRIORITY) ; System.out.println("NORM_PRIORITY = " + Thread.NORM_PRIORITY) ; System.out.println("MIN_PRIORITY = " + Thread.MIN_PRIORITY) ; } };
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i<5;i++){ try{ Thread.sleep(500) ; }catch(Exception e){} System.out.println(Thread.currentThread().getName() + "运行,i = " + i) ; // 取得当前线程的名字 if(i==2){ System.out.print("线程礼让:") ; Thread.currentThread().yield() ; // 线程礼让 } } } }; public class ThreadYieldDemo{ public static void main(String args[]){ MyThread my = new MyThread() ; // 实例化MyThread对象 Thread t1 = new Thread(my,"线程A") ; Thread t2 = new Thread(my,"线程B") ; t1.start() ; t2.start() ; } };
class MyThread extends Thread{ private int time ; public MyThread(String name,int time){ super(name) ; // 设置线程名称 this.time = time ; // 设置休眠时间 } public void run(){ try{ Thread.sleep(this.time) ; // 休眠指定的时间 }catch(InterruptedException e){ e.printStackTrace() ; } System.out.println(Thread.currentThread().getName() + "线程,休眠" + this.time + "毫秒。") ; } }; public class ExecDemo01{ public static void main(String args[]){ MyThread mt1 = new MyThread("线程A",10000) ; // 定义线程对象,指定休眠时间 MyThread mt2 = new MyThread("线程B",20000) ; // 定义线程对象,指定休眠时间 MyThread mt3 = new MyThread("线程C",30000) ; // 定义线程对象,指定休眠时间 mt1.start() ; // 启动线程 mt2.start() ; // 启动线程 mt3.start() ; // 启动线程 } };
class MyThread implements Runnable{ private String name ; private int time ; public MyThread(String name,int time){ this.name = name ; // 设置线程名称 this.time = time ; // 设置休眠时间 } public void run(){ try{ Thread.sleep(this.time) ; // 休眠指定的时间 }catch(InterruptedException e){ e.printStackTrace() ; } System.out.println(this.name + "线程,休眠" + this.time + "毫秒。") ; } }; public class ExecDemo02{ public static void main(String args[]){ MyThread mt1 = new MyThread("线程A",10000) ; // 定义线程对象,指定休眠时间 MyThread mt2 = new MyThread("线程B",20000) ; // 定义线程对象,指定休眠时间 MyThread mt3 = new MyThread("线程C",30000) ; // 定义线程对象,指定休眠时间 new Thread(mt1).start() ; // 启动线程 new Thread(mt2).start() ; // 启动线程 new Thread(mt3).start() ; // 启动线程 } };
标签:
原文地址:http://blog.csdn.net/u013087513/article/details/51628327