JUC系列----线程API介绍(新建线程,终止线程)

2020-04-15  本文已影响0人  小陈中间件

1.新建线程

public class Test1 {
   public static void main(String[] args) {
       Thread thread = new Thread();
       thread.start();
   }
}
public class Test1 {
   public static void main(String[] args) {
       Thread thread = new Thread();
       thread.run();
   }
}
   public static void main(String[] args) {
       Thread thread = new Thread(){
           @Override
           public void run()
           {
               System.out.println("hello Jage");
           }
       };
       thread.start();
   }
public class InheritThreadTest extends Thread {
    @Override
    public void run()
    {
        System.out.println("hello Jage");
    }
}
public class RunnableTest implements Runnable {
    @Override
    public void run() {
        System.out.println("hello Jage");
    }
}
    public Thread(Runnable target) {
        init(null, target, "Thread-" + nextThreadNum(), 0);
    }
public class CreateThread implements Runnable {
    @Override
    public void run() {
        System.out.println("hello Jage");
    }
    public static void main(String[] args) {
        Thread thread = new Thread(new CreateThread());
        thread.start();
    }
}

2.终止线程

数据1:courseId=1,courseName="1"
数据2:courseId=2,courseName="2"

public class StopThreadTest {
   public static Course course = new Course();

    @Data
    public static class Course{
        private Integer id;
        private String name;

        public Course()
        {
            id=0;
            name="0";
        }

        @Override
        public String toString()
        {
            return "Course [id =" + id +", name =" + name +"]";
        }
    }

    public static class ChangeDataThread implements Runnable{

        @Override
        public void run() {
            while (true)
            {
                synchronized (course)
                {
                    int v = (int) (System.currentTimeMillis()/1000);
                    course.setId(v);
                    try
                    {
                        Thread.sleep(100);
                    }catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    course.setName(String.valueOf(v));
                }
                Thread.yield();
            }
        }
    }

    public static class ReadDataThread implements Runnable{

        @Override
        public void run() {
            while (true)
            {
                synchronized (course)
                {
                    if (course.getId() != Integer.parseInt(course.getName()))
                    {
                        System.out.println(course.toString());
                    }
                }
                Thread.yield();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread readThread = new Thread(new ReadDataThread());
        readThread.start();
        while (true)
        {
            Thread changeThread = new Thread(new ChangeDataThread());
            changeThread.start();
            Thread.sleep(150);
            changeThread.stop();
        }
    }
}

Course [id =1586965999, name =1586965998]
Course [id =1586966000, name =1586965999]


public void interrupt() //线程中断
public boolean isInterrupted() //判断线程是否线程中断(true | false)
public static boolean interrupted() //判断线程是否线程中断(true | false),true的话就清除线程中断标志
public class Test1 {
    public static void main(String[] args) throws InterruptedException{
        Thread thread = new Thread(){
            @Override
            public void run()
            {
                while (true)
                {
                    System.out.println("hello Jage");
                    Thread.yield();
                }
            }
        };
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();
    }
}
    public static void main(String[] args) throws InterruptedException{
        Thread thread = new Thread(){
            @Override
            public void run()
            {
                while (true)
                {
                    if (Thread.currentThread().isInterrupted())
                    {
                        break;
                    }
                    System.out.println("hello Jage");
                    Thread.yield();
                }
            }
        };
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();
    }
上一篇 下一篇

猜你喜欢

热点阅读