线程的操作方法

来源:互联网 发布:阿里云有免费空间吗 编辑:程序博客网 时间:2024/05/24 23:11

线程的操作方法
课程大纲
一、 线程的操作方法
(1)、Thread (Runnabel target):分配新的thread对象
(2)、Thread(Runnable target,String name):分配新的thread对象。
(3)、Thread(String name):分配新的thread对象
(4)、static Thread currentThread():返回当前正在执行的线程对象的引用。
(5)、long getId():返回该线程的标示符
(6)、String getName():返回该线程的名称
(7)、void setName(String name):改变线程名称,与name相同。
(8)、boolean isAlive():测试线程是否处于活动状态。
(9)、static void sleep(long millis):休眠指定的毫秒后继续执行。
(10)、static void sleep(long millis,int nanos):休眠指定的毫秒和纳秒后继续执行。
(11)、void join():等待该线程终止。
(12)、void join(long millis):等待该线程终止的时间最长为millis毫秒。
(13)、void join(long millis,int nanos):等待该线程终止的最长时间为指定的毫秒纳秒、
(14)、void interrupt():中断线程
(15)、static Boolean interrupted():测试当前线程是否已经中断。
(16)、void setPriority(int newPriority):更改线程的优先级。
(17)、int getPriority():返回线程的优先级。
(18)、static int MAX_PRIORITY():线程可以具有的最高优先级。
(19)、static int MIN_PRIORITY():线程可以具有的最低优先级
(20)、static int NORM_PRIORITY():分配给线程的默认优先级。
(21)、boolean isDaemon():测试该线程是否是守护线程
(22)、void setDaemon(Boolean on):讲该线程标记为守护线程后者是用户线程。
(23)、static void yield():暂停当前正在执行的线程对象,并执行其他线程。

代码如下:

package us.google;

public class ThreadDemo {

public static void main(String[] args) {    //主线程(main)    System.out.println("获取当前线程的名称:"+Thread.currentThread().getName());    Thread t1 = new Thread(new Mythread(),"小白线程");    System.out.println(t1.getId());    //设置线程的名称    t1.setName("小黑线程");    System.out.println("获取当前线程的名称:"+t1.getName());    //启动线程    System.out.println("线程是否处于存活状态:"+t1.isAlive());    t1.start();//线程启动以后最后会调用run()方法    System.out.println("线程是否处于存活状态:"+t1.isAlive());}

}
class Mythread implements Runnable
{

@Overridepublic void run() {    System.out.println("当前线程叫做:"+Thread.currentThread().getName());}

}
package us.google;

/**
* sleep方法的原理
* 1、让当前线程进入休眠状态,让出当次执行cup的时间,但是该线程不丢失任何监视器的所属权。
* @author chongrubujing
*
*/
public class ThreadDemo2 {

public static void main(String[] args) {    //三个线程争抢cup    Mythread2 my = new Mythread2();    Thread t1 = new Thread(my);    Thread t2 = new Thread(my);    t1.start();    t2.start();    for (int i = 0; i < 10; i++) {        System.out.println(Thread.currentThread().getName()+"-"+i);        try {            //然当前线程进入休眠状态            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

}
class Mythread2 implements Runnable
{

@Overridepublic void run() {    for (int i = 0; i < 10; i++) {        System.out.println(Thread.currentThread().getName()+"-"+i);        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

}

package us.google;

/**
* join()方法:等待当前线程执行终止,或指定的等待时间(毫秒,纳秒)
* @author chongrubujing
*
*/
public class ThreadDemo3 {

public static void main(String[] args)  {    Mythread3 my = new Mythread3();    Thread t1 = new Thread(my);    t1.start();    for (int i = 0; i < 10; i++) {        System.out.println("main-"+i);        if (i==5) {            try {                t1.join();//等待该线程终止(一直让该线程运行结束)            } catch (InterruptedException e) {                e.printStackTrace();            }        }        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

}
class Mythread3 implements Runnable
{

@Overridepublic void run() {    for (int i = 0; i < 10; i++) {        System.out.println(Thread.currentThread().getName()+"-"+i);        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

}

package us.google;

/**
* 中断线程
* 1、interrupt()方法只是设置了线程中断状态为true,并没有真正的中断线程
* 2、自定义标记完成中断线程
* 设置线程的优先级
* @author chongrubujing
*
*/
public class ThreadDemo4 {

public static void main(String[] args)  {    Mythread4 my = new Mythread4();    Mythread5 my1 = new Mythread5();    Thread t1  = new Thread(my,"t1");    Thread t2 = new Thread(my1,"t2");    //设置线程的优先级    t1.setPriority(Thread.MAX_PRIORITY);    t2.setPriority(Thread.MIN_PRIORITY);    t1.start();    t2.start();    for (int i = 0; i <10; i++) {        System.out.println("main-"+i);        if (i==5) {            //t1.interrupt();//中断线程,设置一个中断标记(中断状态为true)            my1.setFalg(false);//中断线程        }        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

}
class Mythread5 implements Runnable
{
private boolean falg = true;

public boolean isFalg() {    return falg;}public void setFalg(boolean falg) {    this.falg = falg;}@Overridepublic void run() {    int i = 0;    while (falg) {        System.out.println(Thread.currentThread().getName()+"-"+i);        i++;        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

}
class Mythread4 implements Runnable
{

@Overridepublic void run() {    int i = 0;    //中断返回true。没有中断返回false    while (!Thread.interrupted()) {        System.out.println(Thread.currentThread().getName()+"-"+i);        i++;        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();            Thread.currentThread().interrupt();        }    }}

}

package us.google;
/**
* 守护线程setDaemon
* 1、yield:暂停当前正在执行的当前线程对象,并执行其他线程
* @author chongrubujing
*
*/
public class ThreadDemo5 {

public static void main(String[] args)  {    Mythread6 my = new Mythread6();    Thread t1 = new Thread(my);    //t1.setDaemon(true);//设置线程为守护线程    //System.out.println(t1.isDaemon());    t1.start();    for (int i = 0; i < 10; i++) {        System.out.println("main-"+i);        if(i==5)        {            Thread.yield();//让出当次CPU的执行时间        }        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

}
class Mythread6 implements Runnable
{

@Overridepublic void run() {    for (int i = 0; i < 20; i++) {        System.out.println("Mythread6-"+i);        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

}

0 0
原创粉丝点击