[Java基础]线程基础与实现多线程

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Java基础]线程基础与实现多线程相关的知识,希望对你有一定的参考价值。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

代码如下:

package MyThreadPack;

public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0;i<100;i++)
        {
            System.out.println(i);
        }
    }
}

package MyThreadPack;

public class MyThreadDemo01 {
    public static void main(String[] args)
    {
        MyThread my1 = new MyThread();
        MyThread my2 = new MyThread();

        my1.start();
        my2.start();
    }
}

在这里插入图片描述

package MyThreadPack;

public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0;i<100;i++)
        {
            System.out.println(getName()+","+i);
        }
    }
}

package MyThreadPack;

public class MyThreadDemo01 {
    public static void main(String[] args)
    {
        MyThread my1 = new MyThread();
        MyThread my2 = new MyThread();

        my1.setName("高铁");
        my2.setName("飞机");

        my1.start();
        my2.start();


    }

}

package MyThreadPack;

public class MyThread extends Thread {

    public MyThread(){}

    public MyThread(String name)
    {
        super(name);
    }


    @Override
    public void run() {
        for (int i = 0;i<100;i++)
        {
            System.out.println(getName()+","+i);
        }
    }
}

package MyThreadPack;

public class MyThreadDemo01 {
    public static void main(String[] args)
    {
        MyThread my1 = new MyThread("高铁");
        MyThread my2 = new MyThread("飞机");



        my1.start();
        my2.start();


    }

}

package MyThreadPack;

public class MyThreadDemo01 {
    public static void main(String[] args)
    {
      System.out.println(Thread.currentThread().getName());

    }

}

在这里插入图片描述

package MyThreadPack;

public class MyThread extends Thread {

    public MyThread(){}

    public MyThread(String name)
    {
        super(name);
    }


    @Override
    public void run() {
        for (int i = 0;i<100;i++)
        {
            System.out.println(getName()+","+i);
        }
    }
}

package MyThreadPack;

public class MyThreadDemo01 {
    public static void main(String[] args)
    {
      MyThread my1 = new MyThread("高铁");
      MyThread my2 = new MyThread("飞机");
      MyThread my3 = new MyThread("汽车");

//      System.out.println(my1.getPriority());
//        System.out.println(my2.getPriority());
//        System.out.println(my3.getPriority());

        //优先级最大为10,最小为1
        my1.setPriority(10);
        my2.setPriority(5);
        my3.setPriority(1);



      my1.start();
      my2.start();
      my3.start();

    }

}

在这里插入图片描述
sleep:
代码如下:

package ThreadSixPack;

public class ThreadSleep extends Thread{
    @Override
    public void run() {
        for (int i = 0;i<100;i++)
        {
            System.out.println(getName()+","+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

package ThreadSixPack;

public class ThreadSleepDemo {
    public static void main(String[] args)
    {
        ThreadSleep ts1 = new ThreadSleep();
        ThreadSleep ts2 = new ThreadSleep();
        ThreadSleep ts3 = new ThreadSleep();

        ts1.setName("Tom");
        ts2.setName("Jack");
        ts3.setName("Bom");

        ts1.start();
        ts2.start();
        ts3.start();

    }

}

join:
代码如下:

package ThreadSixPack;

public class ThreadJoin extends Thread{
    @Override
    public void run() {
        for (int i= 0;i<100;i++)
        {
            System.out.println(getName()+","+i);
        }
    }
}

package ThreadSixPack;

public class ThreadJoinDemo {
    public static void main(String[] args)
    {
        ThreadJoin tj1 = new ThreadJoin();
        ThreadJoin tj2 = new ThreadJoin();
        ThreadJoin tj3 = new ThreadJoin();

        tj1.setName("Tom");
        tj2.setName("Jack");
        tj3.setName("Lily");

        tj1.start();
        try {
            tj1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        tj2.start();
        tj3.start();
    }

}

setDaemon:
代码如下:

package ThreadSixPack;

public class ThreadDaemon extends Thread{
    @Override
    public void run() {
        for (int i  =0;i<100;i++)
        {
            System.out.println(getName()+","+i);
        }
    }
}

package ThreadSixPack;

public class ThreadDaemonDemo {
    public static void main(String[] args)
    {
        ThreadDaemon td1 = new ThreadDaemon();
        ThreadDaemon td2= new ThreadDaemon();

        td1.setName("Tom");
        td2.setName("Jack");


        Thread.currentThread().setName("Lily");

        td1.setDaemon(true);
        td2.setDaemon(true);

        td1.start();
        td2.start();

        for (int i = 0;i<10;i++)
        {
            System.out.println(Thread.currentThread().getName()+","+i);
        }
    }

}

在这里插入图片描述

在这里插入图片描述

package MyRunnablePack;

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        for (int i  =0;i<100;i++)
        {
            System.out.println(Thread.currentThread().getName()+","+i);
        }
    }
}

package MyRunnablePack;

public class MyRunnableDemo {
    public static void main(String[] args)
    {
        MyRunnable my = new MyRunnable();

        Thread t1 = new Thread(my,"高铁");

        Thread t2=  new Thread(my,"飞机");

        t1.start();
        t2.start();
    }

}

以上是关于[Java基础]线程基础与实现多线程的主要内容,如果未能解决你的问题,请参考以下文章

java基础:多线程

[Java] 多线程基础详细总结,附加详细实例

号称史上最全Java多线程与并发面试题总结—基础篇

Java基础之多线程

阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第5节 线程池_2_线程池的代码实现

java多线程基础