Java Hour 27 Concurrent

有句名言,叫做10000小时成为某一个领域的专家。姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧。

27 Hours.

本小时主要过一下多线程相关的基本API.

Defining tasks

package mike.qian.hello;



public class LiftOff implements Runnable {

    protected int countDown = 10; // Default

    private static int taskCount = 0;

    private final int id = taskCount++;



    public LiftOff() {

    }



    public LiftOff(int countDown) {

        this.countDown = countDown;

    }



    public String status() {

        return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!") + "), ";

    }



    public void run() {

        while (countDown-- > 0) {

            System.out.print(status());

            Thread.yield();

        }

    }

}

一个线程主要是为了驱动一个task. 只要实现一个Runnable 接口就表示这是一个task.

Thread.yield 这个是可选了,表明允许CPU从当前线程中滚出去干其他事情去。

package mike.qian.hello;



public class Application {

    public static void main(String[] args) {

        Thread t = new Thread(new LiftOff());

        t.start();

        System.out.println("Waiting for LiftOff");

    }

}
package mike.qian.hello;



import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;



public class Application {

    public static void main(String[] args) {

        ExecutorService exec = Executors.newCachedThreadPool();

        for (int i = 0; i < 5; i++)

            exec.execute(new LiftOff());

        exec.shutdown();

        System.out.println("Waiting for LiftOff");

    }

}

带返回值的线程

package mike.qian.hello;



import java.util.concurrent.Callable;



class TaskWithResult implements Callable<String> {

    private int id;



    public TaskWithResult(int id) {

        this.id = id;

    }



    public String call() {

        return "result of TaskWithResult " + id;

    }

}

package mike.qian.hello;



import java.util.ArrayList;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;



public class Application {

    public static void main(String[] args) {

        ExecutorService exec = Executors.newCachedThreadPool();

        ArrayList<Future<String>> results = new ArrayList<Future<String>>();

        for (int i = 0; i < 10; i++)

            results.add(exec.submit(new TaskWithResult(i)));

        for (Future<String> fs : results)

            try {

                // get() blocks until completion:

                System.out.println(fs.get());

            } catch (InterruptedException e) {

                System.out.println(e);

                return;

            } catch (ExecutionException e) {

                System.out.println(e);

            } finally {

                exec.shutdown();

            }

    }

}

看到这个future, 瞬间石化了,好山寨的命名。

守护线程

守护线程就是后台线程,当所有前台线程退出后,程序将退出。

内部类的使用

package mike.qian.hello;



import java.util.concurrent.*;

import static java.lang.System.out;

// Using a named inner class:

class InnerThread1 {

    private int countDown = 5;

    private Inner inner;



    private class Inner extends Thread {

        Inner(String name) {

            super(name);

            start();

        }



        public void run() {

            try {

                while (true) {

                    out.println(this);

                    if (--countDown == 0)

                        return;

                    sleep(10);

                }

            } catch (InterruptedException e) {

                out.println("interrupted");

            }

        }



        public String toString() {

            return getName() + ": " + countDown;

        }

    }



    public InnerThread1(String name) {

        inner = new Inner(name);

    }

}



// Using an anonymous inner class:

class InnerThread2 {

    private int countDown = 5;

    private Thread t;



    public InnerThread2(String name) {

        t = new Thread(name) {

            public void run() {

                try {

                    while (true) {

                        out.println(this);

                        if (--countDown == 0)

                            return;

                        sleep(10);

                    }

                } catch (InterruptedException e) {

                }

            }



            public String toString() {

                return getName() + ": " + countDown;

            }

        };

        t.start();

    }

}



// Using a named Runnable implementation:

class InnerRunnable1 {

    private int countDown = 5;

    private Inner inner;



    private class Inner implements Runnable {

        Thread t;



        Inner(String name) {

            t = new Thread(this, name);

            t.start();

        }



        public void run() {

            try {

                while (true) {

                    out.println(this);

                    if (--countDown == 0)

                        return;

                    TimeUnit.MILLISECONDS.sleep(10);

                }

            } catch (InterruptedException e) {

                out.println("sleep() interrupted");

            }

        }



        public String toString() {

            return t.getName() + ": " + countDown;

        }

    }



    public InnerRunnable1(String name) {

        inner = new Inner(name);

    }

}



// Using an anonymous Runnable implementation:

class InnerRunnable2 {

    private int countDown = 5;

    private Thread t;



    public InnerRunnable2(String name) {

        t = new Thread(new Runnable() {

            public void run() {

                try {

                    while (true) {

                        out.println(this);

                        if (--countDown == 0)

                            return;

                        TimeUnit.MILLISECONDS.sleep(10);

                    }

                } catch (InterruptedException e) {

                    out.println("sleep() interrupted");

                }

            }



            public String toString() {

                return Thread.currentThread().getName() + ": " + countDown;

            }

        }, name);

    }

}



// A separate method to run some code as a task:

class ThreadMethod {

    private int countDown = 5;

    private Thread t;

    private String name;



    public ThreadMethod(String name) {

        this.name = name;

    }



    public void runTask() {

        if (t == null) {

            t = new Thread(name) {

                public void run() {

                    try {

                        while (true) {

                            out.println(this);

                            if (--countDown == 0)

                                return;

                            sleep(10);

                        }

                    } catch (InterruptedException e) {

                        out.println("sleep() interrupted");

                    }

                }



                public String toString() {

                    return getName() + ": " + countDown;

                }

            };

            t.start();

        }

    }

}



public class Application {

    public static void main(String[] args) {

        new InnerThread1("InnerThread1");

        new InnerThread2("InnerThread2");

        new InnerRunnable1("InnerRunnable1");

        new InnerRunnable2("InnerRunnable2");

        new ThreadMethod("ThreadMethod").runTask();

    }

} /* (Execute to see output) */// :~

 

这里包含了内部类,匿名内部类,相关的内容下个小时补上。

后面茫茫多的内容

其实这块东西和.NET 大同小异,但是东西是茫茫多的,暂且略过不表。

你可能感兴趣的:(Concurrent)