h5做商城网站,成立门户网站建设工作小组,北京网站开发团队,从化网站开发/*** author Alina* date 2021年12月12日 11:37 下午* 线程#xff1a;分为三个级别* 1.定义类#xff0c;继承Thread 类#xff0c;继承线程类* 该类重写run#xff08;#xff09;方法* 创建子类对象* * 执行join方法的线程#xff0c;会一次执行完毕#xff0c;其…/*** author Alina* date 2021年12月12日 11:37 下午* 线程分为三个级别* 1.定义类继承Thread 类继承线程类* 该类重写run方法* 创建子类对象* * 执行join方法的线程会一次执行完毕其他线程在争夺资源* * 线程休眠 sleep 静态* * 线程的让步yield 静态* * 线程的守护setDaemond() 非静态* 最低---1* 默认---5* 最高---10* 2.定义类。继承runnable接口* 重写抽象方法run()* 创建thread 类的对象。Thread 构造方法中传递Runnable接口的实现类对象* 调用thread类的start方法* 利实现接口避免单继承的局限性* 实现接口也可以共享数据* 弊存在安全隐患使用同步技术保证线程安全* 共享数据只有一个线程在运行* 关键字synchronized(任意对象){* 线程操作的共享数据* }******/
class TicketRunnable implements Runnable{private int ticker 1 ;private Object obj new Object();public void run() {synchronized (obj){while (ticker100){try{Thread.sleep(20);}catch (Exception e){}System.out.println(Thread.currentThread().getName()no.....ticker);ticker;}}}
}public class ThreadDemo {public static void main(String[] args) throws Exception{TicketRunnable tp new TicketRunnable();Thread t0 new Thread(tp);Thread t1 new Thread(tp);Thread t2 new Thread(tp);t0.start();t1.start();t2.start();}public static void method_2()throws Exception{SubThread p0 new SubThread();SubThread p1 new SubThread();p0.start();p0.join();p1.start();for (int x0;x100;x){Thread.sleep(100);Thread.yield();System.out.println(Thread.currentThread().getName()......x);}}public static void method_1(){PrioityThead pt new PrioityThead();int x pt.getPriority();System.out.println(x);//获取当前线程的优先级int y Thread.currentThread().getPriority();System.out.println(y);}
}