在劫

吾生也有涯,而知也无涯 。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

抽象类通过内部方法,表示定义抽象类的子类对象:

abstract class Student{
    //抽象类可以通过内部方法创建子类对象
    public Student s1 = new Student(){
        public Student nextOne(){
            return this;
        }
    };
}

在API中枚举类没有values()方法,但实际有这个方法的。enum声明实际上定义了一个类。因此我们可以通过定义的enum调用其方法。其次,Java编译器会自动在enum类型中插入一些方法,其中就包括values()——所以我们的程序在没编译的时候,自然没法查看values()方法的源码了。这个枚举实际上是由java.lang.Enum这个类实现的,在程序中定义的枚举类型,都会隐式继承此类。并且,由于java中的继承是单继承,所以我们定义的枚举就无法在继承其他类了。

    enum Direction {  
        LEFT, RIGHT, UP, DOWN  
    }  


    Direction dirs[] = Direction.values();  
    for (int i = 0; i < dirs.length; i++) {  
         System.out.println(dirs[i]);  
    }  

这样就可以通过下标访问枚举值

1.java中的enum关键字背后实际是Enum这个类实现的。
2.在我们编写自定义的enum时,其中是不含values方法的,再编译java文件时,java编译器会自动帮助我们生成这个方法。

enum Car {
    lamborghini(900),tata(2),audi(50),fiat(15),honda(12);
    private int price;
    Car(int p) {
        price = p;
    }
    int getPrice() {
        return price;
    } 
}
public class Main {
    public static void main(String args[]){
        System.out.println("所有汽车的价格:");
        for (Car c : Car.values())
        System.out.println(c + " 需要 " 
        + c.getPrice() + " 千美元。");
    }
}

可以直接从class文件中获取指定枚举值

Gender g = Enum.valueOf(Gender.class, "FEm");

枚举类的抽象方法

enum Grade  
{  
    //实现抽象方法  
    //隐式添加abstract
    A("100-90")  
    {  
        public String localeVlaue()  
        {  
            return "优";  
        }  
    },  
    B("89-80")  
    {  
        public String localeVlaue()  
        {  
            return "良";  
        }  
    },  
    C("79-70")  
    {  
        public String localeVlaue()  
        {  
            return "中";  
        }  
    },  
    D("69-60")  
    {  
        public String localeVlaue()  
        {  
            return "差";  
        }  
    },  
    E("59-0")  
    {  
        public String localeVlaue()  
        {  
            return "不及格";  
        }  
    };     
//抽象方法
 public abstract String localeVlaue(); 

然后通过枚举对象调用方法

        System.out.println(Grade.A.getValue()+" 分为 "+Grade.A.localeVlaue());  
        System.out.println(Grade.B.getValue()+" 分为 "+Grade.B.localeVlaue());  
        System.out.println(Grade.C.getValue()+" 分为 "+Grade.C.localeVlaue());  
        System.out.println(Grade.D.getValue()+" 分为 "+Grade.D.localeVlaue());  
        System.out.println(Grade.E.getValue()+" 分为 "+Grade.E.localeVlaue());  
        //Collection collections = new HashSet();
     Collection collection = new HashList();
ReflectPoint pt1 = new ReflectPoint(3,3); ReflectPoint pt2 = new ReflectPoint(5,5); ReflectPoint pt3 = new ReflectPoint(3,3); collections.add(pt1); collections.add(pt2); collections.add(pt3); collections.add(pt1); //改变了y的值,hashcode值也改了,所以这里如果调用remove()方法,是无法删掉pt1的 //这叫内存泄漏 //pt1.y = 7; //collections.remove(pt1);
new Thread(
    new Runnable(){
        public void run(){
    //代码执行快
        }
            }
    ){
    public void run(){
    //代码执行
    }
}.start();
//这里执行的是Thread的run()方法,不是Runnable的run()方法

Thread和Runnable两种实现多线程的方式都是本质还是实现Thread类的run()方法

public class TheadRunnableTest {
    public static void main(String[] args) {
        new Thread(
                new Runnable(){
                    public void run() {
                        System.out.println("Runnable.run()方法");
                    }
                }
                ){
            //实现的是Thread的run()方法
            public void run() {
                System.out.println("Thread.run()方法");
            }
        }.start();
    }
}

多线程共享数据

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ThreadScopeShareData {

    private static int data = 0;
    private static Map<Thread, Integer> threadData = new HashMap<Thread, Integer>();
    public static void main(String[] args) {
        for(int i=0;i<2;i++){
            new Thread(new Runnable(){
                @Override
                public void run() {
                    int data = new Random().nextInt();
                    System.out.println(Thread.currentThread().getName() 
                            + " has put data :" + data);
                    threadData.put(Thread.currentThread(), data);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }
    
    static class A{
        public void get(){
            int data = threadData.get(Thread.currentThread());
            System.out.println("A from " + Thread.currentThread().getName() 
                    + " get data :" + data);
        }
    }
    
    static class B{
        public void get(){
            int data = threadData.get(Thread.currentThread());            
            System.out.println("B from " + Thread.currentThread().getName() 
                    + " get data :" + data);
        }        
    }
}

使用ThreadLocal类实现上面程序

import java.util.Random;

public class ThreadScopeShareData {

    private static int data = 0;
    private static ThreadLocal<Integer> tl = new ThreadLocal<Integer>();
    public static void main(String[] args) {
        for(int i=0;i<2;i++){
            new Thread(new Runnable(){
                @Override
                public void run() {
                    int data = new Random().nextInt();
                    System.out.println(Thread.currentThread().getName() 
                            + " has put data :" + data);
                    tl.set(data);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }
    
    static class A{
        public void get(){
            int data = tl.get();
            System.out.println("A from " + Thread.currentThread().getName() 
                    + " get data :" + data);
        }
    }
    
    static class B{
        public void get(){
            int data =tl.get();            
            System.out.println("B from " + Thread.currentThread().getName() 
                    + " get data :" + data);
        }        
    }
}

一个ThreadLocal只能传入一个变量,如果有多个数据就需要定义多个ThreadLocal,所以可以定义一个对象传入到ThreadLocal对象中。

import java.util.Random;

public class ThreadLocalTest {

    private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
    private static ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();
    public static void main(String[] args) {
        for(int i=0;i<2;i++){
            new Thread(new Runnable(){
                @Override
                public void run() {
                    int data = new Random().nextInt();
                    System.out.println(Thread.currentThread().getName() 
                            + " has put data :" + data);
                    x.set(data);
/*                    MyThreadScopeData myData = new MyThreadScopeData();
                    myData.setName("name" + data);
                    myData.setAge(data);
                    myThreadScopeData.set(myData);*/
                    MyThreadScopeData.getThreadInstance().setName("name" + data);
                    MyThreadScopeData.getThreadInstance().setAge(data);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }
    
    static class A{
        public void get(){
            int data = x.get();
            System.out.println("A from " + Thread.currentThread().getName() 
                    + " get data :" + data);
/*            MyThreadScopeData myData = myThreadScopeData.get();;
            System.out.println("A from " + Thread.currentThread().getName() 
                    + " getMyData: " + myData.getName() + "," +
                    myData.getAge());*/
            MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();
            System.out.println("A from " + Thread.currentThread().getName() 
                    + " getMyData: " + myData.getName() + "," +
                    myData.getAge());
        }
    }
    
    static class B{
        public void get(){
            int data = x.get();            
            System.out.println("B from " + Thread.currentThread().getName() 
                    + " get data :" + data);
            MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();
            System.out.println("B from " + Thread.currentThread().getName() 
                    + " getMyData: " + myData.getName() + "," +
                    myData.getAge());            
        }        
    }
}

class MyThreadScopeData{
    private MyThreadScopeData(){}
    public static /*synchronized*/ MyThreadScopeData getThreadInstance(){
        MyThreadScopeData instance = map.get();
        if(instance == null){
            instance = new MyThreadScopeData();
            map.set(instance);
        }
        return instance;
    }
    //private static MyThreadScopeData instance = null;//new MyThreadScopeData();
    private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();
    
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

软件包 java.util.concurrent.atomic

线程池

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //ExecutorService threadPool = Executors.newFixedThreadPool(3);
        //ExecutorService threadPool = Executors.newCachedThreadPool();
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        for(int i=1;i<=10;i++){
            final int task = i;
            threadPool.execute(new Runnable(){
                @Override
                public void run() {
                    for(int j=1;j<=10;j++){
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for  task of " + task);
                    }
                }
            });
        }
        System.out.println("all of 10 tasks have committed! ");
        //threadPool.shutdownNow();
        
        Executors.newScheduledThreadPool(3).scheduleAtFixedRate(
                new Runnable(){
                    @Override
                public void run() {
                    System.out.println("bombing!");
                    
                }},
                6,
                2,
                TimeUnit.SECONDS);
    }

}

 

posted on 2017-06-30 06:24  长嘴大耳怪  阅读(460)  评论(0编辑  收藏  举报