Java多线程

一、线程

  1、线程:程序中单独顺序的控制流

    线程本身依靠程序进行运行

    线程是程序中的顺序控制流,只能使用分配给程序的资源和环境

  2、单线程:程序中只存在一个线程,实际上主方法就是一个主线程

  3、多线程:多线程是在一个程序中运行多个任务,目的:更好的使用CPU

二、线程的实现

  1、继承Thread类   

public class ThreadTest extends Thread {

    @Override
    public void run() {
        super.run();
    }

}


class MainTest{
    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        threadTest.start(); // start方法是开启线程,run方法只是普通的调用
    }
}

  2、实现Runnable接口

public class RunnableTest implements Runnable {
    @Override
    public void run() {
        
    }
}

class MainTest{
    public static void main(String[] args) {
        RunnableTest test = new RunnableTest();
        new Thread(test).start(); // start方法是开启线程,run方法只是普通的调用
    }
}

三、线程的常用方法

  1、取得线程名 getName()  ps:主线程为:main ,其他的:Thread-0等

  2、取得当前线程的对象:currentThread()

  3、判断线程是否启动: isAlive()

  4、线程的休眠:sleep(),参数为毫秒

  5、线程强行运行: join()   可以优先主线程。

public class ThreadTest extends Thread {
    public String gettName() {
        return tName;
    }

    public void settName(String tName) {
        this.tName = tName;
    }

    String tName;

    ThreadTest(String tNamed){
        this.tName = tNamed;
    }


    @Override
    public void run() {
        for (int i = 0;i < 1000;i++){
            System.out.println(tName+" - "+i + " - "+Thread.currentThread().getName());
        }
    }

}


class ThreadTestMain{
    @Test
    public static void threadJoinTest() throws InterruptedException {
        System.out.println(" - "+Thread.currentThread().getName());
        ThreadTest threadTest1 = new ThreadTest("01");
        ThreadTest threadTest2 = new ThreadTest("02");
        threadTest1.start();
        threadTest1.join();
        for (int i = 0;i < 100;i++){
            System.out.println("Thread1 start"+" - "+Thread.currentThread().getName()+" - "+i);
        }
        threadTest2.start();
        for (int i = 0;i < 100;i++){
            System.out.println("Thread2 start"+" - "+Thread.currentThread().getName()+" - "+i);
        }
    }

}

  6、线程的礼让:yield()

public class ThreadTest extends Thread {
    public String gettName() {
        return tName;
    }

    public void settName(String tName) {
        this.tName = tName;
    }

    String tName;

    ThreadTest(String tNamed){
        this.tName = tNamed;
    }


    @Override
    public void run() {
        for (int i = 0;i < 1000;i++){
            System.out.println(tName+" - "+i + " - "+Thread.currentThread().getName());
            if (i == 10){
                System.out.println("yield");
                Thread.yield();
            }
        }
    }

}

四、线程的优先级别

threadTest1.setPriority(Thread.NORM_PRIORITY);  //Thread.MAX_PRIORITY ,Thread.NORM_PRIORITY

五、线程同步(保证是多个线程使用的同一个资源。比如:卖车票,整个车站只有1000张票,三个窗口在卖,要保证不能多卖。ps:如果不同步,可能会出现A、B同时再卖同一张车票,这样不能保证一致性。)

public class SynchronizedDemo implements Runnable {

    private int ticket = 1000;

    public synchronized boolean sell(){
//        try {
//            Thread.sleep(500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        if (ticket > 0){
            System.out.println("Thread object:"+Thread.currentThread()+"- Thread Name:"+Thread.currentThread().getName()+" - 车票:"+ticket--);
            return true;
        }else {
            return false;
        }
    }


    @Override
    public void run() {
        boolean flag = true;
        do {
            flag = sell();
        }while (flag);

    }
}

class ThreadDemo{

    public static void main(String[] args) {
        SynchronizedDemo synchronizedDemo = new SynchronizedDemo();
        Thread threadA = new Thread(synchronizedDemo);
        Thread threadB = new Thread(synchronizedDemo);
        Thread threadC = new Thread(synchronizedDemo);
        threadA.setName("A");
        threadB.setName("B");
        threadC.setName("C");
        threadA.start();
        threadB.start();
        threadC.start();
    }
    
}

 

  

 

posted on 2017-10-17 11:05  chouc  阅读(154)  评论(0编辑  收藏  举报

导航