/**
* 1.自定义一个类,继承thread类
* 2.重写run方法
* 3.调用start方法,开启线程
*/publicclassThreadTest{publicstaticvoid main ( String[] args ){
MyThread mt =newMyThread();
mt.start();//开启一个线程,然后调用run方法,注意,已经开启的线程不能再调用start方法
mt.run();//如果这里调用的时run方法,相当于方法调用,调用了MyThread重写的run方法}}classMyThreadextendsThread{@Overridepublicvoid run (){for(int i =0;i<100;i++){if(i%2==0){
System.out.println(i);}}}}
/**
* 采用匿名内部类的方式开启线程
*/publicclassThreadTest02{publicstaticvoid main ( String[] args ){newThread(){// 打印0-100的偶数@Overridepublicvoid run (){for(int i =0;i<100;i++){if(i%2==0){// Thread.currentThread().getName()获取当前线程的名称
System.out.println(Thread.currentThread().getName()+i);}}}}.start();newThread(){// 打印0-100之间的奇数@Overridepublicvoid run (){for(int i =0;i<100;i++){if(i%2!=0){// Thread.currentThread().getName()获取当前线程的名称
System.out.println(Thread.currentThread().getName()+i);}}}}.start();
System.out.println(Thread.currentThread().getName()+"main线程");}}
2.2 Thread类中的常用方法
1.publicstatic Thread currentThread():返回正在运行的线程的对象的引用;
他具有Thread类的其他成员方法 :
Thread.currentThread().setName():设置主线程名称
2.start():启动当前线程,调用当前线程的run()方法
3.run():通常需要重写Thread类的此方法,将创建的线程需要执行的操作声明再此方法中
4.getName():获取当前线程的名称
5.setName():给当前线程设置名称
6.public final voidjoin()throwInterruptedException():如果线程a调用线程b的join(),此时线程a就会进入阻塞状态,直到线程b执行结束
7.yield():释放当前线程的CPU执行权
8.stop():强制结束当前线程,已过期
9.sleep(long millitime):让当前线程睡眠指定的millitime毫秒,再指定的时间内,线程都处于阻塞状态
10.isAlive():判断当前线程是否存活
11.public final voidsetDaemon(boolean on):将该线程标记为守护线程或用户线程,当正在运行的线程都是守护线程时,
该方法必须在启动线程前调用,形参如果为true。则将该线程标记为守护线程
注意 :守护线程没有主线程无法运行, 主线程停止,守护线程也随之停止
守护线程也和主线程抢占cpu资源
12.线程的优先级
public final int getPriority() 获取线程的优先级
public final voidsetPriority(int Priority):给当前线程设置一个优先级
默认优先级:5
Thread类中存在以下的优先级的常量
publicstatic final int MAX_PRIORITY10 最大
publicstatic final int MIN_PRIORITY1 最小
publicstatic final int NORM_PRIORITY5 默认
classDeamoThreadextendsThread{@Overridepublicvoid run (){for(;;){
System.out.println("我是"+getName());}}}publicclassThreadTest03{publicstaticvoid main ( String[] args ){
DeamoThread deamoThread =newDeamoThread();// 给线程设置一个名称
deamoThread.setName("守护线程");// 设置为守护线程
deamoThread.setDaemon(true);// 设置一个优先级
deamoThread.setPriority(Thread.MAX_PRIORITY);
deamoThread.start();// 给主线程设置名称
Thread.currentThread().setName("主线程");for(int i =0; i <100; i++){
System.out.println(Thread.currentThread().getName()+i);}}}
//Thread.State中定义了线程的五种状态publicenum State {/**
* Thread state for a thread which has not yet started.
*/
NEW,/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;}