Java基础_线程

一、线程的概念

线程:一个程序里面不同的执行路径

publicclassThreadTest1 {

    publicstaticvoid main(String[] args) {

        m1();

    }

    staticvoidm1() {

        m2();

        m3();

    }

    privatestaticvoid m3() {

    }

    privatestaticvoid m2() {

    }

}

进程:静态概念,比如一个class文件或者一个exe文件.通常所说的进城执行了只是它的主线程执行了.比如main主线程

二、线程的创建

2.1 定义线程类实现Runnable接口,并实现线程运行体方法run

注:使用Runnable接口可以为多个线程提供共享数据,在实现Runnable接口的 类的run方法定义中可以使用Thread的静态方法:public static ThreadcurrentThread()获取当前线程的引用;

2.2 定义一个Thread的子类并重写其run方法

publicclassThreadTest2 {

   

    publicstaticvoid main(String[] args) {

        Runner1 r = newRunner1();

        Thread t = newThread(r);

        t.start();

        Runner2 r2 = new Runner2();

        r2.start();

        for(int i=0; i<100;i++) {

            System.out.println("Main Thread: ------ " + i);

        }

    }

}

 

class Runner1implements Runnable {

 

    publicvoidrun() {

        for(int i=0; i<100; i++) {

            System.out.println("Runner1 :" +i);

        }

    }

}

 

class Runner2extendsThread {

    publicvoidrun() {

        for(int i=0; i<100; i++) {

            System.out.println("Runner2>>>>>> : " + i);

        }

    }

}

三、线程的状态


四、线程的操作方法

sleep

public static void sleep(long millis)throws InterruptedException

在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)。该线程不丢失任何监视器的所属权。

参数:

millis- 以毫秒为单位的休眠时间。

抛出:

InterruptedException - 如果另一个线程中断了当前线程。当抛出该异常时,当前线程的中断状态被清除。

interrupt

public void interrupt()

中断线程。

如果线程在调用 Object 类的 wait()wait(long)wait(long,int) 方法,或者该类的 join()join(long)join(long,int)sleep(long)sleep(long,int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException

import java.util.Date;

 

publicclassInterruptTest {

    publicstaticvoid main(String[] args) {

        Runner3 r = newRunner3();

        Thread t = newThread(r);

        t.start();

        try {

            Thread.sleep(10000);

        } catch(InterruptedException e) {

            e.printStackTrace();

        }

        t.interrupt();

    }

}

 

class Runner3implements Runnable {

 

    publicvoidrun() {

        while(true) {

            System.out.println("---" +new Date() +"---");

            try {

                Thread.sleep(1000);

            } catch(InterruptedException e) {

                System.out.println("sleep中的Runner3被打断");

                return;

            }

        }

       

    }

}

注:sleep()和interrupt()让线程退出的一种方式,但是比较粗暴,废弃的stop()更加粗暴,因为interrupt()catch到InterruptedException的时候还可以做一些处理。

import java.util.Date;

 

publicclassInterruptTest {

    publicstaticvoid main(String[] args) {

        Runner3 r = newRunner3();

        Thread t = newThread(r);

        t.start();

        try {

            Thread.sleep(10000);

        } catch(InterruptedException e) {

            e.printStackTrace();

        }

        r.flag =false;

        //t.interrupt();

    }

}

 

class Runner3implements Runnable {

    boolean flag =true;

    publicvoidrun() {

        while(flag) {

            System.out.println("---" +new Date() +"---");

            try {

                Thread.sleep(1000);

            } catch(InterruptedException e) {

                System.out.println("sleep中的Runner3被打断");

                return;

            }

        }

       

    }

}

join

public final void join() throws InterruptedException

等待该线程终止。

抛出:

InterruptedException - 如果另一个线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。

注:将该线程合并到当前线程,相当于在当前线程进行方法调用

publicclassJoinTest {

    publicstaticvoid main(String[] args) {

        Thread1 t = newThread1("thread1");

        t.start();

        try {

            t.join();

        } catch(InterruptedException e) {

            e.printStackTrace();

        }

        for(int i=0; i<5; i++) {

            System.out.println("Thread : " + Thread.currentThread().getName());

        }

    }

}

/*

 *   Thread : thread1

    Thread : thread1

    Thread : thread1

    Thread : thread1

    Thread : thread1

    Thread : thread1

    Thread : thread1

    Thread : thread1

    Thread : thread1

    Thread : thread1

    Thread : main

    Thread : main

    Thread : main

    Thread : main

    Thread : main

 */

class Thread1extendsThread {

    Thread1(String ThreadName) {

        super(ThreadName);

    }

    publicvoidrun() {

        for(int i=0; i<10; i++) {

            System.out.println("Thread : " + Thread.currentThread().getName());

        }

    }

}

yield

public static void yield()

暂停当前正在执行的线程对象,并执行其他线程。

publicclassYieldTest {

    publicstaticvoid main(String[] args) {

        Thread t1 = newThread5("t1");

        Thread t2 = newThread5("t2");

        t1.start();

        t2.start();

    }

}

class Thread5extendsThread {

    Thread5(String s) {

        super(s);

    }

    publicvoidrun() {

        for(int i=1; i<=100; i++) {

            System.out.println(getName()+"-------------" + i);

            if(i%10== 0){

                yield();

            }

        }

    }

}

注:如果你的电脑是多核的,那么运行的时候一定要将MyEclipse所在的CPU关掉一个,否则运行结果不是你想要的,这也正是新的JDK中对线程整体重新做了重大改动的原因之一吧。

五、线程的优先级

Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程。线程调度器按照线程的优先级决定应该调度哪个线程来执行。范围从1到10,默认为5。

publicclassPriorityTest {

    publicstaticvoid main(String[] args) {

        T1 t1 = newT1("thread1");

        T2 t2 = newT2("thread2");

        //t1.setPriority(10);

        t1.setPriority(Thread.NORM_PRIORITY + 3);

        t1.start();

        t2.start();

    }

}

 

class T1extendsThread {

   

    T1(String s) {

        super(s);

    }

    publicvoidrun() {

        for(int i=0; i<1000; i++) {

            System.out.println(getName()+"----------" + i);

        }

    }

}

class T2extendsThread {

   

    T2(String s) {

        super(s);

    }

    publicvoidrun() {

        for(int i=0; i<1000; i++) {

            System.out.println(getName()+"----------" + i);

        }

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值