public class Test extends Thread{
@Override
public void run() {
System.out.println("Test thread");
}
public static void main(String[] args) {
new Test().start();
}
}
public class Test implements Runnable{
@Override
public void run() {
System.out.println("Test thread");
}
public static void main(String[] args) {
Thread t1 = new Thread(new Test());
t1.start();
}
}
线程的中断
public void interrupt() //通知线程中断,设置中断标志位,但是由线程来决定处理方式
public boolean isInterrupted() //判断线程是否被中断,也就是检查线程有无中断标志位
public static boolean interrupted() //判断线程是否被中断,如果是,清除中断标志位
线程的等待和通知
public final void wait() //Object的方法之一,调用之后,线程必须等待通知,必须获得锁时才能调用这个方法
public final native void notify() //也是Object的方法之一,也必须获得互斥锁才能调用这个方法,随机通知一个线程并唤醒它就绪
public final native void notifyAll() //同上,不过可以唤醒所有等待的线程
线程的合作
public final synchronized void join() //主线程等待当前线程结束
public static native void yield() //线程主动退出cpu,状态有运行变为就绪
Java多线程编程基础如何创建线程继承java.lang.Thread类和实现java.lang.Runnable接口public class Test extends Thread{ @Override public void run() { System.out.println("Test thread"); } public static void main(Strin...