class MyThread extends Thread { private volatile boolean running = true; public void stopRunning() { running = false; } @Override public void run() { while (running) { // 线程执行的逻辑 } }} 4.4 使用并发容器 Java提供了一系列线程安全的并发容器,如Concurrent...
4.3 数组的基本操作 遍历数组 public class Output { public static void main(String[] args){ int arr[][]=new int[][]{{1,2,3},{4,5},{6}}; for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ System.out.print(arr[i][j]); } System.out.println(); } ...
while ((s = br.readLine()) != null) { System.out.println(s); } } catch (Exception e) { System.err.println(e.getMessage()); // handle exception } finally { if (br != null) { try { br.close(); } catch(Throwable t) { /* ensure close happens */ } } if (r != null)...
privatestaticvoidreadFile(String filePath)throws IOException{File file=newFile(filePath);String result;BufferedReader reader=newBufferedReader(newFileReader(file));while((result=reader.readLine())!=null){System.out.println(result);}reader.close();} Throws抛出异常的规则: 如果是不可查异常(unchecked e...
while(true){ try{//2.调用锁定方法 lock() lock.lock(); if(ticket > 0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":售票,票号为:" + ticket); ticket--; }else{ break; } }finally { ...
熟悉switch(byte|short|int|String|enum){case xx: yyy break },for循环(特别是两层嵌套)、while(条件){循环体;步长;},以及break和continue的用法和场景; 为什么数组获取长度用length,字符串获取长度用length(); Object类中常用的方法:getClass(),hashCode(),equals(),clone(),toString(),finalize()垃圾回收前...
while (it.hasNext()) { System.out.println(it.next()); list.add("AA");//边迭代边添加 会出现并发修改异常 } } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. ...
网络爬虫——基于JAVA的宽度优先遍历互联网结点 整个的宽度优先爬虫过程就是从一系列的种子节点开始,把这些网页中(种子结点网页)的“子节点” (也就是超链接)提取出来,放入队列中依次进行抓取。被处理过的链接需要放入一张表(通常称 为 Visited 表)中。每次新处理一个链接之前,需要查看这个链接是否已经存在于 ...
while (it.hasNext()) { //遍历集合 UpdateStu stu = (UpdateStu) it.next(); System.out.println(stu.getId() + " " + stu.getName()); } it = tree.headSet(stu2).iterator(); //截取stu2之前的对象 System.out.println("截取前面部分的集合:"); while (it.hasNext()) { //遍历集合 Upda...
fail-fast(快速失败):快速失败机制在遍历一个集合时,如果集合内容被修改,会抛出ConcurrentModificationException异常。 fail-safe(安全失败):安全失败机制对集合的任何修改都会在一个复制的集合上进行,因此不会抛出异常。 9、get 和 post请求的区别 get: 1、请求的参数会附加在URL之后,多个参数用 & 连接。 2、因为...