标签:java 线程状态 线程同步 runnable thread
Java实现多线程的方法有两种,一是继承Thread类、二是实现Runnable接口。
一、继承Thread类
public class ThreadTest extends Thread { @Override public void run() { System.out.println("this thread name is:"+Thread.currentThread().getName()); } /** * @param args */ public static void main(String[] args) { ThreadTest t = new ThreadTest(); t.setName("myTestThread"); t.start(); } }
二、实现Runnable接口
public class RunnableTest implements Runnable { @Override public void run() { System.out.println("this thread name is:"+Thread.currentThread().getName()); } public static void main(String[] args) { System.out.println(Thread.currentThread().getName()); RunnableTest r = new RunnableTest(); Thread t = new Thread(r); t.start(); } }
三、线程的状态转换
四、线程同步各个方法的区别
标签:java 线程状态 线程同步 runnable thread
原文地址:http://blog.csdn.net/wtyvhreal/article/details/44176369