推广措施,如何优化关键词搜索,优化算法 网站,设计首页//利用两个线程实现1到100的计算
public class MyRannable implements java.lang.Runnable{private Thread th_0;private Thread th_2;int sum0;//存储累加和的结果int i1;public void run(){String thNameThread.currentThread().getName();//获取当前线程的名字while (i…//利用两个线程实现1到100的计算
public class MyRannable implements java.lang.Runnable{private Thread th_0;private Thread th_2;int sum0;//存储累加和的结果int i1;public void run(){String thNameThread.currentThread().getName();//获取当前线程的名字while (i101){System.out.println( 当前线程thName正在计算 );System.out.println( 当前的累加和 sum);sumi;if(i50thName.equals( 线程1 )){break;}try {Thread.sleep( 100 );}catch (InterruptedException e){e.printStackTrace();}}}public MyRannable(Thread t0,Thread t2){if(t0null){th_0new Thread( this );//th0和th2共享一个实现runnable的实例}if(t2null){th_2new Thread( this );}th_0.setName( 线程1 );th_2.setName( 线程2 );th_0.start();//启动线程t0try {th_0.join();}catch (InterruptedException e){e.printStackTrace();}th_2.start();}
}测试类
public class test106 {public static void main(String[] args){//Thread t0new Thread( );//t0.setName( 线程1 );//Thread t2new Thread( );//t2.setName( 线程2 );Thread t0null;Thread t2null;MyRannable rannew MyRannable( t0,t2 );}
}运行结果