1、遍历Jar/zip文件中的资源
- File jar = new File("myfile.jar");
- ZipInputStream zis = new ZipInputStream(new FileInputStream(jar));
- try {
- for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
- // visit entry
- }
- } finally {
- zis.close();
- }
2、遍历WEB应用中的资源。
Set<String> subResources = servletContext.getResourcePaths("/WEB-INF/");
3、自定义属性编辑器,date的格式化中常用
- public class MyPropertyEditor extends PropertyEditorSupport {
- String format = "yyyy-MM-dd";
- public void setFormat(String format) {
- this.format = format;
- }
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- SimpleDateFormat dateFormat = new SimpleDateFormat(format);
- System.out.println("--->" + text);
- try {
- Date date = dateFormat.parse(text);
- this.setValue(date);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
4、this关键字总结
调用构造函数,且必须放在构造函数的第一行,且不能产生循环构造函数调用。
- class Person {
- private String username;
- private String password;
- public Person() {
- System.out.println("Person construct.");
- }
- public Person(String username) {
- this();
- this.username = username;
- }
- public Person(String username, String password) {
- this(username);
- this.password = password;
- }
- }
this对象
- public void print(){
- System.out.println(this);
- }
- Person person = new Person("zhangsan","hello");
- person.print();
- System.out.println(person);
测试结果:属性的内容完全一致,具体如下。说明this代表的就是当前new出来的对象。new出来的是哪个对象this就代表这个对象。
- com.alibaba.hummock.designpattern.composite2.Person@1fb8ee3
- com.alibaba.hummock.designpattern.composite2.Person@1fb8ee3
5、输出系统属性到屏幕,不用再一个个的for循环输出
System.getProperties().list(System.out);
- class MyThread implements Runnable {
- private String threadName;
- public MyThread(String threadName) {
- super();
- this.threadName = threadName;
- }
- public void run() {
- for (int i = 0; i < 5; i++) {
- System.out.println(threadName);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public class Junit {
- public static void main(String[] args) throws Exception {
- MyThread target1 = new MyThread("mt1");
- Thread thread = new Thread(target1);
- thread.start();
- thread.join();
- for (int i = 0; i < 5; i++) {
- System.out.println(Thread.currentThread().getName());
- }
- }
- }
- mt1
- mt1
- mt1
- mt1
- mt1
- main
- main
- main
- main
- main
- public class Junit {
- public static void main(String[] args) throws Exception {
- MyThread target1 = new MyThread();
- MyThread target2 = new MyThread();
- target1.start();
- target2.start();
- }
- }
- class MyThread extends Thread {
- private static int count = 10;
- public synchronized void run() {
- while (count > 0) {
- System.out.println(Thread.currentThread().getName() + ": " + count--);
- try {
- Thread.sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
或者:
- public class Junit {
- public static void main(String[] args) {
- MyThread target1 = new MyThread();
- MyThread target2 = new MyThread();
- Thread thread1 = new Thread(target1);
- Thread thread2 = new Thread(target2);
- thread1.setName("t1");
- thread2.setName("t2");
- thread1.start();
- thread2.start();
- }
- }
- class MyThread implements Runnable {
- private static int count = 10;
- public synchronized void run() {
- while (count > 0) {
- System.out.println(Thread.currentThread().getName() + " sell : " + count--);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
测试结果:
- Thread-0: 10
- Thread-1: 9
- Thread-1: 8
- Thread-0: 7
- Thread-0: 6
- Thread-1: 5
- Thread-0: 4
- Thread-1: 3
- Thread-1: 2
- Thread-0: 1
【注意】
第一:线程之间共享的变量必须是成员变量,而不是方法中的变量,且必须属于线程的,即必须为static变量
第二:线程之间必须同步,即synchronized
18、多线程中线程安全问题
在程序中,成员变量是可以被多个成员方法操作(读或写),即便该变量存在加锁(synchronized)的方法中,在多线程的情况下也可以被其他线程调用其他方法修改,这也就是成员变量不是线程安全的根本原因。servlet为线程不安全性即为最好例子。如果要使用成员变量,又要保证线程安全性,那该成员变量在其他方法中必须为只读,不能修改该变量。
- public class Junit {
- public static void main(String[] args) throws Exception {
- Ticket ticket = new Ticket();
- new Thread(ticket).start();
- //保证线程已经被启动
- Thread.sleep(1000);
- ticket.update();
- }
- }
- class Ticket implements Runnable {
- private /*static*/ int ticket = 100;
- synchronized public void run() {
- ticket = 1000;
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("ticket: " + ticket);
- }
- public void update() {
- ticket--;
- System.out.println(ticket);
- }
- }
测试结果:
- 999
- ticket: 999
【注意】如果同一个类中有多个方法都使用了synchronized关键字,就要分析他们获取的是哪个对象的锁。如果是同一把锁,则只能被一个线程获取;否则可并行执行。