Java小测试

    public class ThreadExit {
        public static void main(String[] args) throws InterruptedException {
            Exit T = new Exit();
            T.start();
            Thread.sleep(10*1000);
            T.setLoop(false);
        }
    }
    
    class Exit extends Thread {
        private boolean loop = true;
        public int a = 0;
    
        public void run() {
            while (loop) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程进行中" + (++a));
            }
            System.out.println("Thread is exiting");
        }
    
        public void setLoop(boolean loop) {
            this.loop = loop;
        }
    }
方法
  • setName()方法用于设置线程的名字,在线程启动时,会调用setName()方法,
  • getName()方法用于获取线程的名字
  • start()方法用于启动线程
  • run()方法用于线程的执行体
  • join()方法用于阻塞当前线程,直到调用线程结束
  • interrupt()方法用于中断线程,并不会结束线程
  • setPriority()方法用于设置线程的先级
  • getPriority()方法用于获取线程的优先级
  • sleep()方法用于阻塞当前线程,直到调用线程结束,休眠
  • yield()方法用于让出CPU时间片,让其他线程执行,让出CPU时间片,但不一定成功
  • setDaemon()方法用于设置线程是否为守护线程,守护线程会在主线程结束时结束
    public class ThreadMethod {
        public static void main(String[] args) throws InterruptedException {
            //获取线程名称的方法:Thread.currentThread().getName()
            //守护线程
            OtherThread otherThread = new OtherThread();
            otherThread.setDaemon(true);
            otherThread.start();
            for (int i = 0; i < 3; i++) {
                System.out.println("主线程正在运行");
                Thread.sleep(1000);
            }
            System.out.println("主线程结束,使用了setDaemon()方法,子线程也结束了");
        }
    }
    
    class OtherThread extends Thread {
        /**
         * 用户线程:
         * 线程结束后以通知的方式提醒
         * 守护线程:  (垃圾回收机制)
         * 线程结束后不提醒
         */
        int i = 0;
        @Override
        public void run() {
    
            for (;;) {
                i++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(i);
            }
        }
    }
多线程售票
    public class Ticketsill {
        public static void main(String[] args) {
            //创建连个对象去访问同一个锁
            Ticket t = new Ticket();  //创建一个对象
            Thread thread = new Thread(t); //创建一个线程
            thread.setName("窗口1");  //设置线程名称
            Thread thread2 = new Thread(t);
            thread2.setName("窗口2");
            thread.start(); //启动线程
            thread2.start();
    
        }
    }
    class Ticket implements Runnable{
        private int ticketNum=100;
        //synchronized 修饰方法,锁住的是对象
        //使用synchronized 可以保证线程安全,保证每次只有一个线程可以访问
        //但必须是同一个对象,才能锁的住
        @Override
        public  void run() {
            while (true) {
                synchronized (this) {
                    if (ticketNum <= 0) {
                        System.out.println("没票了");
                        break;
                    }
                    //
                    System.out.println(Thread.currentThread().getName()+"拿到了 " +(ticketNum--)+"号票");
                }
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            }
        }
    }
    //改三次
最后修改:2022 年 10 月 04 日
如果觉得我的文章对你有用,请随意赞赏