在java中 多线程的实现有两种方式:
1.扩展 java.lang.Thread方法
class Mythread extends Thread{
public void run()
{
//覆写run();
}
定义主类 实例化线程跌对象,发动启动线程的命令
public class ThreadText{
public static void main(string args[])
{
Mythread m=new Mythread();
m.start();
}
}
2.实现java.lang.runnable方法
定义一个线程类继承runnable接口
class mythread implements Runnable{
public void main()
{
// 线程的主体
}
public class ThreadText{
public static void main(string args[])
{
Mythread m=new Mythread();
m.start();
}
}
每个线程都有一个优先级,高的先与低的,都能可以或不可以标识为守护程序,当某个线程中运行的代码创建一个新的Thread对象时,该新线程的初始优先级被设定为创建线程的优先级,并且仅当创建的是守护线程时,新线程才是守护线程。