public class Thread1 extends Thread {private int j;// 重写run方法public void run() {for (; j < 100; j++) {System.out.println(getName() + " " + j);}}// 主函数public static void main(String args[]) {for (int i = 0; i < 100; i++) {// 打印主线程执行信息System.out.println(Thread.currentThread().getName() + " " + i);if (i == 20) {// 新启两个线程new Thread1().start();new Thread1().start();}}}}public class Thread2 implements Runnable {private int j;// 实现run方法public void run() {for (; j < 100; j++) {System.out.println(Thread.currentThread().getName() + " " + j);}}// 主方法public static void main(String args[]) {for (int i = 0; i < 100; i++) {// 打印主线程执行信息System.out.println(Thread.currentThread().getName() + " " + i);if (i == 20) {// 新启两个线程Thread2 thread=new Thread2();new Thread(thread,"新线程1").start();new Thread(thread,"新线程2").start();}}}}
//同步方法 同步监视器为thispublic synchronized int getM1(int count){return 1;}
//同步代码块public void getM2(){synchronized(obj){//代码块,obj为同步监视器}}
private Lock lock=new ReentrantLock();//在方法内使用同步锁public void getM3(){//开始锁lock.lock();//同步区。。。try{}finally{//释放锁lock.unlock();}}
class A {synchronized void foo(B b) {String name = Thread.currentThread().getName();System.out.println(name + " entered A.foo");try {Thread.sleep(1000);} catch (Exception e) {System.out.println("A Interrupted");}System.out.println(name + " trying to call B.last()");// 因为b对象被锁住了,调用b内的方法时,在等锁的释放b.last();}synchronized void last() {System.out.println("Inside A.last");}}class B {synchronized void bar(A a) {String name = Thread.currentThread().getName();System.out.println(name + " entered B.bar");try {Thread.sleep(1000);} catch (Exception e) {System.out.println("B Interrupted");}System.out.println(name + " trying to call A.last()");// 因为a对象被锁住了,调用a内的方法时,在等锁的释放a.last();}synchronized void last() {System.out.println("Inside A.last");}}class Deadlock implements Runnable {A a = new A();B b = new B();Deadlock() {Thread.currentThread().setName("MainThread");Thread t = new Thread(this, "RacingThread");t.start();a.foo(b); // 同步监视器为aSystem.out.println("Back in main thread");}public void run() {b.bar(a); // 同步监视器为bSystem.out.println("Back in other thread");}public static void main(String args[]) {new Deadlock();}}
//生产消费模型public class Main {public static void main(String args[]) {List<Egg> list = new ArrayList<Egg>();new Product(list).start();new Customer(list).start();}}public class Egg {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String toString() {return id + " " + name;}}public class Product extends Thread {private List<Egg> list;private int count;public Product(List<Egg> list) {this.list = list;}// 重写run方法public void run() {System.out.println("生产线程启动");while (true) {try {Thread.sleep(100);synchronized (list) {//还有鸡蛋时while (list.size() > 0) {list.wait();}//没有鸡蛋时while(list.size()==0){Egg egg=new Egg();egg.setId(count++);egg.setName("鸡蛋");System.out.println("生产线程生产"+egg.toString());list.add(egg);//通知消费线程list.notify();}}} catch (InterruptedException e) {e.printStackTrace();}}}}public class Customer extends Thread {private List<Egg> list;public Customer(List<Egg> list) {this.list = list;}// 重写run方法public void run() {System.out.println("消费线程启动");while (true) {try {Thread.sleep(100);synchronized (list) {//没有则等待while (list.size() == 0) {list.wait();}//有鸡蛋时while(list.size()>0){System.out.println("消费线程消费"+list.remove(0).toString());//通知生产线程list.notify();}}} catch (InterruptedException e) {e.printStackTrace();}}}}
//监控模型public class Control implements Runnable {// 存放统计线程的队列private static List<CountFile> list = new ArrayList<CountFile>();// 主函数public static void main(String args[]) {// 取得系统的根目录个数java.io.File[] dirF = java.io.File.listRoots();// 根据目录创建统计纯种个数for (int i = 0; i < dirF.length; i++) {CountFile cf = new CountFile(dirF[i].getAbsolutePath());cf.start();list.add(cf);}System.out.println(dirF.length + " 个统计线程已启动");// 启动监视线程new Thread(new Control()).start();System.out.println("监视线程已启动");}// 实现run方法public void run() {boolean flag = true;String result = "";while (flag) {for (int i = 0; i < list.size(); i++) {if (list.get(i).isFinished()) {// 取得统计结果result += list.get(i).getResult();// 移出统计完的线程list.remove(i);}}// 全部统计完if (list.isEmpty()) {flag = false;}try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}}System.out.println("统计结果如下:");System.out.println(result);}}public class CountFile extends Thread {private String root;// 进行搜索的根目录的名字private int lengthCount;// 所有文件长度private int dirCount;// 目录数private int realFileCount;// 统计的真正文件数量private boolean finished = false;// 构造时传入搜索根目录名public CountFile(String root) {this.root = root;}/*** 查看线程是否统计结束** @return*/public boolean isFinished() {return finished;}/*** 返回统计结果** @return*/public String getResult() {StringBuffer result = new StringBuffer();result.append(root + "盘统计结果如下:\r\n");result.append(" 文件数量: " + realFileCount);result.append(" 目录数: " + dirCount);result.append(" 文件总长度(单位K): " + lengthCount / 1204);result.append("\r\n");return result.toString();}// 重写run方法public void run() {long start = System.currentTimeMillis();lengthCount = countProcess(root);long cost = System.currentTimeMillis() - start;finished = true;}/*** 统计目录下文件的长度** @param root* 要统计的目录* @return 目录下文件的长度*/private int countProcess(String root) {int count = 0;File dirFile = new File(root);// 目录不存在if (!dirFile.exists()) {return count;}// 获得目录下的所有文件组成的数组File[] subFile = dirFile.listFiles();if (subFile == null) {return count;}// 对这个数组进行遍历for (int i = 0; i < subFile.length; i++) {// 是个目录if (subFile[i].isDirectory()) {dirCount++;count += countProcess(subFile[i].getAbsolutePath());// 用递归计算该目录下的文件长度}// 是一个文件if (subFile[i].isFile()) {realFileCount++;count += subFile[i].length();}}return count;}}
联系客服