北在北方

太白枝头看,花开不计年,杯中浮日月,楼外是青天。
随笔 - 200, 文章 - 0, 评论 - 239, 阅读 - 68万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

随笔分类 -  JavaSE

摘要:Semaphore,信号量。 用在多线程环境下对共享资源访问的一种协调机制。 当一个线程想要访问共享的资源时,这个线程需要获取Semaphore,如果Semaphore内部计数器的值大于0,Semaphore就会减少内部计数器的值且允许这个线程访问共享资源; 如果Semaphore内部计数器的值等于0,说明共享资源正在被其他线程访问,就禁止这个线程访问,需等待其他线程释放Semaphore后才能访问。public class Counter { //计数器,共享的资源 public static int count = 0; //声明Semaphor... 阅读全文

posted @ 2013-10-31 16:39 CN.programmer.Luxh 阅读(390) 评论(0) 推荐(0) 编辑

摘要:1、使用synchronizedpackage cn.luxh.app.test;import java.util.LinkedList;import java.util.List;import java.util.Random;import java.util.concurrent.Executor;import java.util.concurrent.Executors;/** * synchronized实现的生产者和消费者 */public class ProducerCustomerWithSynchronized { Executor pool = Execu... 阅读全文

posted @ 2013-09-04 14:33 CN.programmer.Luxh 阅读(4319) 评论(0) 推荐(0) 编辑

摘要:冒泡排序、选择排序和插入排序代码如下:package cn.luxh.app.test;public class SimpleSortUtil { /** * 冒泡排序 * 从小到大排序 * 思路: * 1)第一趟排序进行了len-1次比较,数组中的最大值元素排到了最末端,位置已固定,该元素不再参与下趟比较 * 2)第二趟排序进行了len-2次比较,因为只需要比较到第一个位置固定的元素(第一趟排序中的最大值元素)即可,数组中的第二大值元素位置也已固定,该元素不再参与下趟比较 * 3)第三趟排序进行了len-3次比较,因为只需要比较到... 阅读全文

posted @ 2013-07-18 16:02 CN.programmer.Luxh 阅读(461) 评论(1) 推荐(2) 编辑

摘要:package cn.luxh.app;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.nio.charset.Charset;import org.junit.Test;public class NioTester { /** * 读取文件 * 获取通道;创建缓冲区;让通道将数据读到... 阅读全文

posted @ 2013-06-14 10:28 CN.programmer.Luxh 阅读(409) 评论(0) 推荐(0) 编辑

摘要:要生成和解析如下格式的xml文件:<?xml version="1.0" encoding="UTF-8"?><Message xmlns:xs="http://www.w3.org/2001/XMLSchema"> <User> <id>1</id> <name>李寻欢</name> <age>30</age> <Address> <id>1</id> <province>广东省 阅读全文

posted @ 2013-01-10 19:46 CN.programmer.Luxh 阅读(3917) 评论(0) 推荐(0) 编辑

摘要:有两个数组,需要找出这两个数组之间相同的元素。package cn.luxh.jpa.test;import java.util.HashSet;import java.util.Set;import org.junit.Test;public class ArrayTest { /** * 找出两个数组中相同的元素 * @param array1 * @param array2 * @return 返回相同的元素 */ public Set<Integer> findSameElementIn2Arrays(Integer[] ... 阅读全文

posted @ 2012-09-21 11:26 CN.programmer.Luxh 阅读(2753) 评论(2) 推荐(1) 编辑

摘要:Apache Commons Compress是一个压缩、解压缩文件的类库。 可以操作ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200 and bzip2格式的文件,功能比较强大。 在这里写两个用Commons Compress把文件压缩成zip和从zip解压缩的方法。 直接贴上工具类代码:package cn.luxh.utils;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.... 阅读全文

posted @ 2012-06-28 21:46 CN.programmer.Luxh 阅读(18731) 评论(6) 推荐(0) 编辑

摘要:昨天一位项目组同事需要在ArrayList中删除所有不等于指定值的元素,但是她弄了很久,发现总是删得不完全。刚好我以前做过类似的功能,就告诉她是因为ArrayList删除元素后长度变小了,元素的索引也会跟着改变,但是迭代的下标没有跟着相应的改变的缘故。 将一些删除方法做一些总结: 1 /** 2 * 删除Arraylist中值为"c"的元素 3 */ 4 public static void main(String[] args) { 5 6 List<String> list = new ArrayList<String... 阅读全文

posted @ 2012-05-19 23:48 CN.programmer.Luxh 阅读(23796) 评论(10) 推荐(3) 编辑

摘要:java.io.File 类有个方法可以直接重命名文件。 方法:public boolean renameTo(File dest)。 1 File file1=new File("D:/test/a.txt"); 2 File file2=new File("D:/test/b.txt"); 3 boolean flag = file1.renameTo(file2); file1的文件名将重命名为file2的文件名,即a.txt重命名为b.txt。 在重命名前,如果已存在b.txt,则重命名不会成功,方法执行结果返回false。 必须确保目标文件在重 阅读全文

posted @ 2012-05-17 21:56 CN.programmer.Luxh 阅读(6443) 评论(3) 推荐(1) 编辑

摘要:public void writeData2Txt(String content) { //存放文件的目录 String fileDir = ServletActionContext.getServletContext().getRealPath("D:/file/"); //简单的生成文件名 String fileName =new SimpleDateFormat("yyyyMMddHHmmsszzz").format(new Date())+".txt"; File txtFile= new File(fileDir,fileN 阅读全文

posted @ 2012-05-14 14:05 CN.programmer.Luxh 阅读(546) 评论(2) 推荐(0) 编辑

摘要:Map<Object,Object> map = new HashMap<Object,Object>();Iterator<Entry<Object,Object>> iterator = map.entrySet().iterator();while(iterator.hasNext()) { Entry<Object,Object> entry = iterator.next(); Object key = entry.getKey(); Object value = entry.getValue();}Entry存的是Map的 阅读全文

posted @ 2012-05-14 13:09 CN.programmer.Luxh 阅读(519) 评论(0) 推荐(1) 编辑

点击右上角即可分享
微信分享提示