java学习笔记之线程篇

2017-12-12 16:29:40

什么是线程
  • 有序严谨的指令集称为程序。
  • 程序的同时多运行称为进程。
  • 程序中不同的执行路径称为线程。

多线程编程实现功能的代码量少,效率高,易于资源共享。但是编写好并不空易,理解起来比较困难。

什么是线程同步
  • 线程同步是指在一段程序执行过程中,无论成功还是失败,其它线程都会等待这段程序执行完毕,才会转入其它线程。这样可以保证程序的完整性和安全性。

下面是两个简单的示例代码

public class Test
{
    public static void main(String[] args)
    {
        Xc xc = new Xc();
        xc.start(); //谁调用的这个方法,程序就会去自动调用run方法
        //start会单开启一个线程,而不是直接调用
        for (int i = 0; i < 20; i++) {
            System.out.println("主函数");
        }
    }

}

class Xc extends Thread {

    public void run()
    {
        for (int i = 0; i < 20; i++) {
            System.out.println("子函数");
        }
    }

}
public class Test
{
    public static void main(String[] args)
    {
        XC xc = new XC();
        Thread a = new Thread(xc);
        a.start();

        for (int i = 0; i < 20; i++) {
            System.out.println("主函数");
        }
    }
}

class XC implements Runnable
{


    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("子函数");
        }
    }
}


线程名的查看于创建
public class Test
{
    public static void main(String[] args)
    {
        XC xc1 = new XC();
        xc1.setName("线程1");
        xc1.start();

        XC xc2 = new XC();
        xc2.setName("线程2");
        xc2.start();

        XC xc3 = new XC();
        xc3.setName("线程3");
        xc3.start();

        System.out.println(Thread.currentThread().getName()+"在执行");
    }
}



class XC extends Thread
{
    public void run()
    {
        //显示当前线程名称
        System.out.println("正在执行:" + Thread.currentThread().getName());
    }
}
线程的优先级
public class Test
{
    public static void main(String[] args)
    {
        Thread xc1 = new Thread(new Xc1());
        Thread xc2 = new Thread(new Xc2());

        //设置优先级,线程默认的级别是5,数字越大优先级越高
        xc2.setPriority(Thread.NORM_PRIORITY + 3);

        xc1.start();
        xc2.start();
    }
}

class Xc1 implements Runnable
{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("1线程" + i);
        }
    }
}

class Xc2 implements Runnable
{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("2线程" + i);
        }
    }
}

体验一下,效果不佳

线程的睡眠
public class Test
{
    public static void main(String[] args)
    {
        Xc1 xc1 = new Xc1();
        Thread cc = new Thread(xc1);
        cc.start();
        
    }
}

class Xc1 implements Runnable
{
    @Override
    public 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();
            }
        }
    }
}

用throws拋异常的时候,如果向主调处拋异常的方法是从父类继承的或者是从接口实现的,那么,覆盖父类方法或实现接口方法时,如果父类中的原方法或接口中的原抽象方法没有拋异常,则子类覆盖父类的方法或实现接口的方法也不能拋异常

当出现这种情况时,只能try...catch,大不了catch中什么也不写。

线程的让步
public class Test
{
    public static void main(String[] args)
    {
        XC a = new XC();
        Thread aa = new Thread(a);
        Thread bb = new Thread(a);

        aa.setName("线程一");
        bb.setName("线程二");

        aa.start();
        bb.start();


    }
}

class XC implements Runnable
{

    @Override
    public void run() {
        for (int i = 0; i < 30; i++) {
            System.out.println(Thread.currentThread().getName() + ": "+i);
            if(i % 5 == 0)
            {
                Thread.yield();
            }
        }
    }
}
线程的阻塞
public class Test
{
    public static void main(String[] args)
    {

        Xc1 xc = new Xc1();
        Thread a = new Thread(xc);
        a.start();

        try {
            //阻塞是将当前线程暂停,直至调用join函数所对应的线程执行完毕
            //才继续执行程序
            a.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 25; i++) {
            System.out.println("主线程: " + i);
        }

    }
}

class Xc1 implements Runnable
{

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("子线程: " + i);
        }
    }
}


线程之间资源共享及同步实例
class Xc9 implements Runnable
{
    public static int chepiao = 100;
    String aa = new String("1");


    public void run()
    {
        while (true)
        {
            synchronized (aa)
            {
                if (chepiao > 0)
                {
                    System.out.println("名称为:"+Thread.currentThread().getName()+"的车站正在卖,"+(101-chepiao)+"张车票");
                    --chepiao;
                }
                else
                {
                    break;
                }
            }
        }
    }
}

public class Test
{
    public static void main(String[] args)
    {
        Xc9 xc9=new Xc9();
        Thread ee = new Thread(xc9);
        ee.start();

        Thread ff = new Thread(xc9);
        ff.start();
    }
}
public class Test
{
    public static void main(String[] args)
    {
        Xc xc = new Xc();
        xc.start();

        Xc xc1 = new Xc();
        xc1.start();
    }
}

class Xc extends Thread
{
    public static int chepiao = 100;
    public static String aa = new String("1");

    @Override
    public void run() {
        while (true)
        {
            synchronized (aa)
            {
                if(chepiao > 0)
                {
                    System.out.println("第"+Thread.currentThread().getName()+"个车站正在卖出"+(101-chepiao)+"张车票");
                    --chepiao;
                }else
                {
                    break;
                }
            }
        }
    }
}