【注】
windows设置环境变量,例如设置jdk为1.7.0版本(一定要把%path%加上):
set path="D:\ProgramFiles\Java\jdk1.7.0\bin";%path%
1、取得当前进程下的所有线程
- public static String[] getThreadNames() {
- ThreadGroup group = Thread.currentThread().getThreadGroup();
- ThreadGroup parent = null;
- while ((parent = group.getParent()) != null) {
- group = parent;
- }
- Thread[] threads = new Thread[group.activeCount()];
- group.enumerate(threads);
- java.util.HashSet<String> set = new java.util.HashSet<String>();
- for (int i = 0; i < threads.length; ++i) {
- if (threads[i] != null && threads[i].isAlive()) {
- try {
- set.add(threads[i].getThreadGroup().getName() + ","
- + threads[i].getName() + ","
- + threads[i].getPriority());
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- }
- String[] result = (String[]) set.toArray(new String[0]);
- java.util.Arrays.sort(result);
- return result;
- }
参考:http://3ccoder.iteye.com/blog/581476
- public class ImportThread extends Thread {
- private CountDownLatch threadsSignal;
- public ImportThread(CountDownLatch threadsSignal) {
- this.threadsSignal = threadsSignal;
- }
- @Override
- public void run() {
- System.out.println(Thread.currentThread().getName() + "开始...");
- //Do somethings
- threadsSignal.countDown();//线程结束时计数器减1
- System.out.println(Thread.currentThread().getName() + "结束. 还有" + threadsSignal.getCount() + " 个线程");
- }
- }
main:
- CountDownLatch threadSignal = new CountDownLatch(threadNum);//初始化countDown
- for (int ii = 0; ii < threadNum; ii++) {//开threadNum个线程
- final Iterator<String> itt = it.get(ii);
- Thread t = new ImportThread(itt,sql,threadSignal);
- t.start();
- }
- threadSignal.await();//等待所有子线程执行完
- System.out.println(Thread.currentThread().getName() + "结束.");//打印结束标记
3、生产者消费者问题
- public class ProducerTest {
- public static void main(String[] args) {
- Queue q = new Queue();
- Producer t1 = new Producer(q);
- Consumer t2 = new Consumer(q);
- t1.start();
- t2.start();
- }
- }
- class Producer extends Thread {
- Queue q;
- public Producer(Queue q) {
- this.q = q;
- }
- public void run() {
- for (int i = 0; i < 10; i++) {
- q.put(i);
- System.out.println("Producer put:" + i);
- }
- }
- }
- class Consumer extends Thread {
- Queue q;
- public Consumer(Queue q) {
- this.q = q;
- }
- public void run() {
- while (true) {
- System.out.println("Consumer get:" + q.get());
- }
- }
- }
- class Queue {
- int value;
- boolean bFull = false;
- public synchronized void put(int i) {
- if (!bFull) {
- value = i;
- bFull = true;
- notify();
- }
- try {
- wait();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public synchronized int get() {
- if (!bFull) {
- try {
- wait();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- bFull = false;
- notify();
- return value;
- }
- }
4、Comparable接口使用
- class Student implements Comparable
- {
- int num;
- String name;
- static class StudentComparator implements Comparator
- {
- public int compare(Object o1,Object o2)
- {
- Student s1=(Student)o1;
- Student s2=(Student)o2;
- int result=s1.num > s2.num ? 1 : (s1.num==s2.num ? 0 : -1);
- if(result==0)
- {
- result=s1.name.compareTo(s2.name);
- }
- return result;
- }
- }
- Student(int num,String name)
- {
- this.num=num;
- this.name=name;
- }
- public int compareTo(Object o)
- {
- Student s=(Student)o;
- return num > s.num ? 1 : (num==s.num ? 0 : -1);
- }
- public String toString()
- {
- return num+":"+name;
- }
- }
如要求先name,后age,则可参考如下:
- class Student implements Comparable<Object> {
- private String name;
- private int age;
- public Student(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- @Override
- public int compareTo(Object o) {
- Student student = null;
- if (o instanceof Student) {
- student = (Student) o;
- }
- int ret1 = this.name.compareTo(student.name);
- // int ret2 = this.age - student.age;
- return ret1 != 0 ? ret1 : (this.age > student.age ? 1
- : (this.age == student.age ? 0 : -1));
- }
- }
5、配置文件覆盖方法
- String requestedFile = System.getProperty(PROPERTIES_FILE);
- String propFileName = requestedFile != null ? requestedFile
- : "quartz.properties";
6、static final
一个既是static又是final的字段只占据一段不能改变的存储空间。 带有恒定初始值(即,编译时常量)的static final基本类型全用大写字母命名,并且字与字之间用下划线隔开。如java.lang.byte类里对byte类型的最小值定义:
public static final byte MIN_VALUE = -128;
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/655642,如需转载请自行联系原作者