1.第一步:创建一个子线程继承主线程;
2.第二步:重写主线程中的Run方法;
3.第三步:在主线程中new一个子线程实例,
4.第四部:在main方法中调用子线程的start方法;
常用线程方法1.currentThread();静态方法,返回执行当前代码的线程
2.getName();获取当前线程的名字;
3.setName();创建当前线程的名字;
4.yeild();释放掉CPU当前线程;
5.join();线程a调用线程b的join(),线程a处于阻塞状态,当线程b执行完成后,.
线程a阻塞状态结束;
6.sleep();给线程休眠时间;
7.currentThread();
代码Public class Thread{
public static void main(String[] args){
HelloThread h1 = new HelloThread1(name:"Thread":1);
h1.start;
//s1.getName("主线程");
for (int i<0; i<100; i++){
if(i%2 !==0) {
System.out.println(Thread.currentThread().getName() + ":" +i);
}
if(i==20){
try{
h1.join();
}catch(InterruptedException e){
e.printStackTrace;
}
}
}
class MyThread extends Thread{
@Override
public void run(){
for (int i<0; i<100; i++){
if(i%2 ==0) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public HelloThread(String name){
super(name)
}
}
=====================================================================
//class Thread {
public Thread(String name) {
}
public static void main(String[] args) {
HelloThread h1 = new HelloThread();
h1.start();
//s1.getName("主线程");
for (int i=0;i< 100;i++){
if (i % 2 != 0) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
if (i == 20) {
try {
h1.join();
} catch (InterruptedException e) {
e.printStackTrace;
}
}
}
class HelloThread extends Thread {
public void run() {
for (int i=0; i< 100;i++){
if (i % 2 == 0) {
System.out.println(HelloThread.currentThread().getName() + ":" + i);
}
}
}
public HelloThread(String name) {
super(name);
}
}
}
}



