//两条线程交替进行//创建线程方式一:继承Theard类,重写run方法,调用start开启线程//总结:注意,线程开启不一定立即执行,由CPU调度执行packagecom.zeng.demo01;publicclassTestTheard01extendsThread{@Overridepublicvoidrun(){//run方法线程体for(int i =0; i <20; i++){System.out.println("hello --"+i);}}publicstaticvoidmain(String[] args){//main线程,主线程//创建一个线程对象TestTheard01 testTheard01 =newTestTheard01();//调用start方法开启线程testTheard01.start();for(int i =0; i <20; i++){System.out.println("hello word--"+i);}}}
Thread thread = new Thread(testThread02);
thread.start();
packagecom.zeng.demo01;//创建线程方法2:实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法publicclassTestThread02implementsRunnable{@Overridepublicvoidrun(){//run方法线程体for(int i =0; i <200; i++){System.out.println("hello woeld--"+i);}}publicstaticvoidmain(String[] args){//创建runnable接口的实现类对象TestThread02 testThread02 =newTestThread02();//创建线程对象,通过线程对象来开启我们的线程代理// Thread thread = new Thread(testThread02);// thread.start();newThread(testThread02).start();for(int i =0; i <200; i++){System.out.println("hello--"+i);}}}
如果要进行四舍五入,可以对浮点数加上0.5再强制转型:
package HomeTest01;public class Main {public static void main(String[] args){double a 0.6;System.out.println((int)(a0.5));double b 0.4;System.out.println((int)(b0.5));}}C也同理&…