浏览 1636 次
锁定老帖子 主题:java基础
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2018-03-15  

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Test {

 public static void main(String[] args) throws Exception{
  List list = new ArrayList();
  List l = new LinkedList();
  HashMap m;
  Hashtable t;
  TreeMap tr;   //红黑树
  
  HashSet hs;
  int i = (25*3)/2 ;
  System.out.println(i);
  
  Integer in;
  
  
    Properties props = System.getProperties();
          Object s = props.get("java.lang.Integer.IntegerCache.high");
          System.out.println(s);
         
        Integer i1 = new Integer(2000);
        System.out.println(i1 == 2000);
        System.out.println(i1.equals(2000));
       
        Student s1 = new Student();
        Student s2 = new Student();
       
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
       
        String t1 = new String("a");
        String t2 = new String("a");
        System.out.println(t1 == t2);
        System.out.println(t1.equals(t2));
       
        String t11 = new String("a");
        String t21 = "a";
        System.out.println(t21);
        System.out.println(t11.equals(t21));
       
        Map<Student, Object> mp = new HashMap<Student, Object>(); //hashCode()
        mp.put(s1, null);
        //s2.setId(1);
        mp.put(s2, null);
        System.out.println(mp.size());
       
        //线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        
        Thread th1 = new PrintThread("t1");
        Thread th2 = new PrintThread("t2");
        Thread th3 = new PrintThread("t3");
        Thread th4 = new PrintThread("t4");
        Thread th5 = new PrintThread("t5");
       
        executorService.execute(th1);
        executorService.execute(th2);
        executorService.execute(th3);
        executorService.execute(th4);
        executorService.execute(th5);
        executorService.shutdown();
       
        //
        ExecutorService exe = Executors.newFixedThreadPool(1);
        Thread ta1 = new PrintThread("ta1");
        Thread ta2 = new PrintThread("ta2");
        Thread ta3 = new PrintThread("ta3");
        Thread ta4 = new PrintThread("ta4");
        Thread ta5 = new PrintThread("ta5");
        exe.execute(ta1);
        exe.execute(ta2);
        exe.execute(ta3);
        exe.execute(ta4);
        exe.execute(ta5);
        exe.shutdown();
       
        //
        ExecutorService exe2 = Executors.newFixedThreadPool(2);
        Thread tb1 = new PrintThread("tb1");
        Thread tb2 = new PrintThread("tb2");
        Thread tb3 = new PrintThread("tb3");
        Thread tb4 = new PrintThread("tb4");
        Thread tb5 = new PrintThread("tb5");
        exe2.submit(tb1);
        exe2.submit(tb2);
        exe2.submit(tb3);
        exe2.submit(tb4);
        exe2.submit(tb5);
        exe2.submit(tb5);
        exe2.submit(tb5);
        exe2.shutdown();
       
        //返回值
        ExecutorService exe3 = Executors.newSingleThreadExecutor();

        Set<Callable<String>> callables = new HashSet<Callable<String>>();

        callables.add(new Callable<String>() {
            public String call() throws Exception {
                return "Task 1";
            }
        });
        callables.add(new Callable<String>() {
            public String call() throws Exception {
                return "Task 2";
            }
        });
        callables.add(new Callable<String>() {
            public String call() throws Exception {
                return "Task 3";
            }
        });

        System.out.println("..");
        List<Future<String>> futures = exe3.invokeAll(callables);
        System.out.println("....");
        for(Future<String> future : futures){
            System.out.println("future.get = " + future.get());
        }
        System.out.println("......");
        exe3.shutdown();
       
        //调度
     // 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。 
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); 
        // 创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口 
        Thread t31 = new PrintThread("s31"); 
        Thread t32 = new PrintThread("s32"); 
        Thread t33 = new PrintThread("s33"); 
        // 将线程放入池中进行执行 
        pool.execute(t31); 
        // 使用延迟执行风格的方法 
        pool.schedule(t32, 1000, TimeUnit.MILLISECONDS); 
        pool.schedule(t33, 10, TimeUnit.MILLISECONDS); 
 
        // 关闭线程池 
        pool.shutdown(); 
       
       
 }

}

class Student {
 private int id;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 @Override
 public boolean equals(Object obj) {
  boolean f = false;
  if(obj instanceof Student) {
   Student s2 = (Student) obj;
   if(this.id == s2.getId()) {
    f = true;
   }
  }
   
  return f;
 }

 @Override
 public String toString() {
  return String.valueOf(this.id);
 }

 @Override
 public int hashCode() {
  return this.id;
 }
}

class PrintThread extends Thread{
 public PrintThread(String name) {
  super(name);
 }
 public void run() {
  int t = new Random().nextInt(1000);
  try {
   Thread.sleep(t);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  System.out.println(this.getName() + "  " + t);
 }
}

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics