多线程
main( )方法是主线程入口,关键词Thread
一、线程改名
public static void main(String[] args) {
Thread t=new Thread("线程1");
Thread th=Thread.currentThread();
th.setName("主线程");
System.out.println(th.getName());
System.out.println(t.getName());
}
二、线程创建
线程的创建方式:
1. 继承Thread类
2. 实现Runnable接口
3. 实现Callable接口
方法1:
继承Thread类
public void run(){
for (int i = 1; i <=100 ; i++) {
System.out.println(Thread.currentThread().getName()
+":"+i);
}
}
public static void main(String[] args) {
// TestThread t1=new TestThread();
// t1.setName("线程1");
// TestThread t2=new TestThread();
// t2.setName("线程2");
// t1.start();
// t2.start();
TestThread t=new TestThread();
Thread th1=new Thread(t);
th1.setName("线程1");