标签:ora override group 显示 int 简介 add string 法线
/** * @Author YangHe * @Date 2020/4/7 11:52 */ public class ThreadTest extends Thread { private int j; public ThreadTest(String name){ super(name); } public ThreadTest(ThreadGroup threadGroup,String name){ super(threadGroup,name); } @Override public void run() { for(;j<100;j++){ System.out.println(getName()+" "+j +" "+getThreadGroup().activeCount()); //显示该线程组活跃的线程数 if(j==50){ Thread.yield(); //当变量值为50 放弃当前资源 进入就绪状态 /* try { Thread.sleep(1000); //当变量值为50 睡眠一秒 进入阻塞状态 } catch (InterruptedException e) { e.printStackTrace(); }*/ } } } public static void main(String[] args) { ThreadTest t=new ThreadTest("6666666666"); t.setDaemon(true);//该线程设置为后台线程,所有线程结束该线程结束 t.start(); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+" "+i); if(i==20){ ThreadGroup threadGroup=new ThreadGroup("测试");//新建线程组 ,设置线程组名称 ThreadTest t1=new ThreadTest(threadGroup,"杨贺"); ThreadTest t2=new ThreadTest("李小艺"); t1.start(); t2.start(); } } } }
/** * @Author YangHe * @Date 2020/4/7 11:52 */ public class RunnableTest implements Runnable{ private int j; @Override public void run() { for(;j<100;j++){ System.out.println(Thread.currentThread().getName()+" "+j); } } public static void main(String[] args) { for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+" "+i); if(i==20){ RunnableTest r=new RunnableTest(); new Thread(r,"杨贺").start(); new Thread(r,"李小艺").start(); } } } }
import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; /** * @Author YangHe * @Date 2020/4/7 15:47 */ public class CallableTest implements Callable<Integer> { @Override public Integer call() throws Exception { int i=0; for(;i<100;i++){ System.out.println(Thread.currentThread().getName()+" "+i); } return i; } public static void main(String[] args) throws InterruptedException { CallableTest callableTest=new CallableTest(); FutureTask<Integer> task=new FutureTask<Integer>(callableTest); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"循环变量的值为 "+i); if(i==20){ Thread t1=new Thread(task,"李小艺"); t1.start(); t1.join(); //等待该线程执行完成,主线程再执行 } } try{ System.out.println("子线程的返回值为 "+task.get()); }catch (Exception e){ e.printStackTrace(); } } }
标签:ora override group 显示 int 简介 add string 法线
原文地址:https://www.cnblogs.com/yanghe123/p/12660474.html