线程常用的方法:
getName() 返回线程的名字
package com.cn.thread;public class Demo3 extends Thread{ public Demo3(String name) { super(name);//初始化线程的名字 } @Override public void run(){// System.out.println("this:" + this);// System.out.println("当前对象:" + Thread.currentThread()); for(int i=0; i<100; i++){ System.out.println(this.getName() + "\t" + i); //getName() 返回线程的名字 /* try { Thread.sleep(100);//在这里不能抛出异常,只能捕获。因为Thread类的run()方法没有抛出异常类型,所以子类不能抛出异常类型。 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ } } public static void main(String[] args) throws InterruptedException { Demo3 d = new Demo3("线程1");// Demo3 d1 = new Demo3("线程2");// d.sleep(1000*2);//让当前正在执行的线程休眠(暂停执行),,,,这里是主线程在睡眠// d.sleep(1000*2);//让当前正在执行的线程休眠(暂停执行),,,,这里还是主线程在睡眠// d.start();//开启线程// d1.setName("木先森"); //setName(String name) 设置线程对象名// d1.start();// System.out.println("线程1的优先级:" + d.getPriority());// System.out.println("线程2的优先级:" + d1.getPriority());// Thread mainThread = Thread.currentThread();// System.out.println("主线程的名字:" + mainThread.getName()); System.out.println("d:" + d); d.setPriority(10); //优先级的数字越大,优先级越高,优先级的范围是1~10 Thread.currentThread().setPriority(1); d.start(); /* System.out.println(Thread.currentThread());//返回对当前正在执行的线程对象的引用。 System.out.println("自定义线程的优先级:" + d.getPriority());//线程的优先级默认是5 System.out.println("主线程的优先级:" + Thread.currentThread().getPriority());*/ for(int i=0; i<100; i++){ System.out.println(Thread.currentThread().getName() + i); } }}