顯示具有 Java SE::Thread 應用 標籤的文章。 顯示所有文章
顯示具有 Java SE::Thread 應用 標籤的文章。 顯示所有文章

2015年10月12日 星期一

Java 的 Thread 基本應用

繼承 Thread :
public class ExampleThread extends Thread{
    @Override
    public void run() {
        for(int i = 0; i<100; i++){
            System.out.println("i: " + i);
        }
    }
}

public class Demo {
    public static void main(String[] args){
        ExampleThread t1 = new ExampleThread();
        t1.start();
    }
}

執行 Runnable 介面 :
public class ExampleRunnable implements Runnable{

    @Override
    public void run() {
        for(int i = 0; i<100; i++){
            System.out.println("i: " + i);
        }
    }  
}

public class RunnableDemo {
     public static void main(String[] args){
        ExampleRunnable r1 = new ExampleRunnable();
        Thread t1 = new Thread(r1);
        t1.start();
    }
}

2015年8月29日 星期六

Java 並行 API

Java 的 java.util.concurrent.atomic 應用:
import java.util.concurrent.atomic.*;

public class Demo{

 public static void main(String[] args){
  
  AtomicInteger ai = new AtomicInteger(5);
  
  if (ai.compareAndSet(5,42)){
   System.out.println("The current value: " + ai.get());
   System.out.println("Replaced 5 with 42");
  }
 }
}

Concurrent I/O 的應用:
public class MultiThreadedClientMain {
    public static void main(String[] args){
        ExecutorService es = Executors.newCachedThreadPool();
        Map < RequestResponse,Future < RequestResponse >> callables = 
                                                     new HashMap<>();
        
        String host = "localhost";
        for (int port = 10000; port <  10010 ; port++){
            RequestResponse lookup = new RequestResponse(host, port);
            NetworkClientCallable callable = 
                                  new NetworkClientCallable(lookup);
            Future future = es.submit(callable);
            callables.put(lookup, future);
        }
        es.shutdown();
        try{
            es.awaitTermination(5, TimeUnit.SECONDS);
        } catch(InterruptedException ex){
            System.out.println("Stopped waiting early");
        }
        
        for (RequestResponse lookup : callables.keySet()){
            Future future = callables.get(lookup);
            try {
                lookup = future.get();
                System.out.println(lookup.host + ":" + lookup.port + 
                     " " + lookup.response);
            } catch (ExecutionException | InterruptedException ex){
                System.out.println("Error talking to " + lookup.host +
                        ":" + lookup.port);
            }
        }
    }
}

public class RequestResponse {
    public String host;
    public int port;
    public String response;
    
    public RequestResponse(String host, int port){
        this.host = host;
        this.port = port;
    }
}

public class NetworkClientCallable implements Callable < RequestResponse > {
    private RequestResponse lookup;
    
    public NetworkClientCallable(RequestResponse lookup) {
        this.lookup = lookup;
    }

    @Override
    public RequestResponse call() throws IOException {
        try (Socket sock = new Socket(lookup.host, lookup.port);
             Scanner scanner = new Scanner(sock.getInputStream());){
            lookup.response = scanner.next();
        }
        return lookup;
    }   
}

2015年8月22日 星期六

volatile 與 synchronized 使用

volatile 範例:
public class ExampleRunnable implements Runnable{
    private volatile int j;
    @Override
    public void run() {
        
        for (j = 0; j < 10; j+=2){
            System.out.println("j = " + j);
        }
    }   
}

Demo 一下,看看執行結果:
public class ThreadDemo {
    public static void main(String[] args){ 

        ExampleRunnable er1 = new ExampleRunnable();

        Thread t1 = new Thread(er1);
        Thread t2 = new Thread(er1);
        Thread t3 = new Thread(er1);
        t1.start();
        t2.start();
        t3.start();
        
    }
}


syhchronized 範例: