课堂随笔
8/13
线程:串行;多线程使电脑运行效率加快,有利于并发和并行;
无论是if...else循环还是for...if循环,都是一条一条的执行的,但是效率不高
sleep:让程序等待一段时间
System.out.print("开始"); Thread.sleep(4000); System.out.println("继续");
结果:开始
(中间停留4秒)
继续
System.out.print("开始"); Thread.sleep(4000); System.out.println(Thread.currentThread()); System.out.println("继续");
结果:
开始
Thread[main,5,main]
继续
线程也是一个对象。
package com; class MyThread implements Runnable { @Override public void run() { try { //休眠 Thread.sleep((long)Math.floor(Math.random()*10)*1000); } catch (InterruptedException e) { e.printStackTrace(); } //输出休眠的线程名称 System.out.println(Thread.currentThread()); System.out.println("在新的线程内运行的任务。这任务是独立的,不会影响主线程代码执行。"); } } public class test { public static void main(String...args) { //创建一个任务对象 MyThread t1 = new MyThread(); Thread t2 = new Thread(t1); //开始任务 t2.start(); } }
结果:
![](https://images2018.cnblogs.com/blog/1409899/201808/1409899-20180813102603531-563569953.png)
Math.random()方法
java.util.Random
以s结尾的类里面有很多静态的方法
线程池
常量出现的目的就是为了优化
currentTimeMillis()方法
8/15:
DOM SAX dom4j了解
json