Concurrency (一) prac1.

2019-03-03  本文已影响0人  lhsjohn
Concurrency.jpg

并发多线程Demo清单

1.定义任务,定义一个任务显示发射之前的倒计时


package com.lhsjohn.comcurrency.prac;

public class LiftOff implements Runnable {

    protected int countDown = 10;
    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")+"),";
    }

    @Override
    public void run() {
        while(countDown-->0) {
            System.out.println(status());
            Thread.yield();
        }
    }

}



标识符id可以用来区分任务的多个实例,因为它是final的,一旦被初始化后就不希望被修改。
在run()中对静态方法Thread.yield()的调用是对线程调度器的一种建议,它是Java线程机制的一部分,可以将CPU从一个线程转移给另外一个线程。

2.MainThrad.java

package com.lhsjohn.comcurrency.prac;

public class MainThread {

      public static void main(String[] args) {
        LiftOff launch = new LiftOff();
        launch.run();
    }
}


运行结果:

#0(9),
#0(8),
#0(7),
#0(6),
#0(5),
#0(4),
#0(3),
#0(2),
#0(1),
#0(liftOff),


这里的run()不是由单独的线程驱动的,它是在main()中直接调用的,使用了分配给main()的那个线程
记住:要实现线程行为,你必须显式地将一个任务附着到线程上。

3.下面展示如何用Thread来驱动LiftOff对象

package com.lhsjohn.comcurrency.prac;

public class BasicThreads {
   
    public static void main(String[] args) {
        Thread t = new Thread(new LiftOff());
        t.start();
        System.out.println("Waiting for LiftOff");
    }
}


输出结果:

Waiting for LiftOff
#0(9),
#0(8),
#0(7),
#0(6),
#0(5),
#0(4),
#0(3),
#0(2),
#0(1),
#0(liftOff),

在这里我们可以看到,Waiting for LiftOff消息在倒计时完成之前就出现了,这是因为我们产生的是对LiftOff.run()的方法的调用,并且这个方法还没有完成,是因为这个方法是有不同的线程执行的,所以我们依旧可以进行main()线程中的其他操作。(任何线程都可以启动其他线程)

4.添加更多线程驱动更多任务

package com.lhsjohn.comcurrency.prac;

public class MoreBasicThreads {
    public static void main(String[] args) {
        
        for(int i=0;i<5;i++) {
            new Thread(new LiftOff()).start();
        }
        System.out.println("Waiting for Liftoff!!");
    }
}


输出结果:

#0(9),
#0(8),
#0(7),
#0(6),
#0(5),
#0(4),
#0(3),
#4(9),
#4(8),
#4(7),
#2(9),
#3(9),
#1(9),
Waiting for Liftoff!!
#1(8),
#3(8),
#2(8),
#4(6),
#0(2),
#4(5),
#2(7),
#3(7),
#1(7),
#3(6),
#2(6),
#4(4),
#0(1),
#0(liftOff),
#4(3),
#2(5),
#3(5),
#3(4),
#3(3),
#3(2),
#3(1),
#3(liftOff),
#1(6),
#1(5),
#2(4),
#4(2),
#4(1),
#4(liftOff),
#2(3),
#1(4),
#2(2),
#1(3),
#2(1),
#1(2),
#2(liftOff),
#1(1),
#1(liftOff),


输出说明不同任务的执行在线程被换进换出时混在了一起。这种交换是由线程调度器自动控制的。

这个程序一次运行的结果可能和另一次运行的结果不同,因为线程调度机制是非确定性的。

5.使用CachedThreadPool为每个任务都创建一个线程

package com.lhsjohn.comcurrency.prac;

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

public class CachedThreadPool {
  
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        for(int i = 0;i<5;i++) {
            exec.execute(new LiftOff());
            
        }
        exec.shutdown();
    } 
    
}



输出结果:


#1(9),
#0(9),
#0(8),
#2(9),
#2(8),
#4(9),
#4(8),
#3(9),
#4(7),
#4(6),
#4(5),
#4(4),
#2(7),
#0(7),
#0(6),
#0(5),
#0(4),
#0(3),
#0(2),
#0(1),
#1(8),
#0(liftOff),
#2(6),
#4(3),
#3(8),
#4(2),
#4(1),
#4(liftOff),
#2(5),
#1(7),
#2(4),
#3(7),
#3(6),
#3(5),
#3(4),
#3(3),
#3(2),
#3(1),
#3(liftOff),
#2(3),
#1(6),
#2(2),
#1(5),
#2(1),
#1(4),
#2(liftOff),
#1(3),
#1(2),
#1(1),
#1(liftOff),


6.使用 FixedThreadPool使用有限线程集执行所提交的任务


package com.lhsjohn.comcurrency.prac;

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

public class FixedThreadPool {
    
    public static void main(String[] args) {
        ExecutorService exec = Executors.newFixedThreadPool(5);
        for(int i = 0; i<5; i++) {
            exec.execute(new LiftOff());
        }
        exec.shutdown();
    }
}


运行结果:

#1(9),
#4(9),
#3(9),
#2(9),
#0(9),
#2(8),
#3(8),
#1(8),
#4(8),
#1(7),
#3(7),
#2(7),
#0(8),
#2(6),
#3(6),
#1(6),
#4(7),
#1(5),
#3(5),
#2(5),
#0(7),
#2(4),
#3(4),
#1(4),
#4(6),
#1(3),
#3(3),
#2(3),
#0(6),
#2(2),
#3(2),
#1(2),
#4(5),
#1(1),
#3(1),
#2(1),
#0(5),
#2(liftOff),
#3(liftOff),
#1(liftOff),
#4(4),
#0(4),
#4(3),
#0(3),
#4(2),
#0(2),
#4(1),
#0(1),
#4(liftOff),
#0(liftOff),


通过FixedThreadPool 我们可以一次性预先执行代价高昂的线程分配,因而可以限制线程的数量了。

7.使用SingleThreadExector可以让多个任务进行排队

package com.lhsjohn.comcurrency.prac;

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

public class SingleThreadExecutor {

     public static void main(String[] args) {
        ExecutorService exec = Executors.newSingleThreadExecutor();
        for(int i = 0;i<5;i++) {
            exec.execute(new LiftOff());
        }
        exec.shutdown();
    }
}


运行结果


#0(9),
#0(8),
#0(7),
#0(6),
#0(5),
#0(4),
#0(3),
#0(2),
#0(1),
#0(liftOff),
#1(9),
#1(8),
#1(7),
#1(6),
#1(5),
#1(4),
#1(3),
#1(2),
#1(1),
#1(liftOff),
#2(9),
#2(8),
#2(7),
#2(6),
#2(5),
#2(4),
#2(3),
#2(2),
#2(1),
#2(liftOff),
#3(9),
#3(8),
#3(7),
#3(6),
#3(5),
#3(4),
#3(3),
#3(2),
#3(1),
#3(liftOff),
#4(9),
#4(8),
#4(7),
#4(6),
#4(5),
#4(4),
#4(3),
#4(2),
#4(1),
#4(liftOff),



8.实现一个Runnable 。在run()内部打印一条消息,然后调用yield()。重复这个操作三次,然后从run中返回。在构造器中放置一条启动消息,并且放置一条在任务终止时的关闭的消息。使用线程创建大量的这种任务并驱动它们。

package com.lhsjohn.comcurrency.prac;

class Ex1RunnerA implements Runnable{

    public Ex1RunnerA() {
        System.out.println("Constructing Ex1RunnerA");
    }
    
    @Override
    public void run() {
        for(int i = 0 ;i<3;i++) {
            System.out.println("Hi from Ex1RunnerA");
            Thread.yield();
        }
        System.out.println("Ex1RunnerA task complete");
        return;
    }
    
    
        
}

class Ex1RunnerB implements Runnable{

    public Ex1RunnerB() {
        System.out.println("Constructing Ex1RunnerB");
    }
    
    @Override
    public void run() {
        for(int i = 0 ;i<3;i++) {
            System.out.println("Hi from Ex1RunnerB");
            Thread.yield();
        }
        System.out.println("Ex1RunnerB task complete");
        return;
    }
            
}


class Ex1RunnerC implements Runnable{

    public Ex1RunnerC() {
        System.out.println("Constructing Ex1RunnerC");
    }
    
    @Override
    public void run() {
        for(int i = 0 ;i<3;i++) {
            System.out.println("Hi from Ex1RunnerC");
            Thread.yield();
        }
        System.out.println("Ex1RunnerC task complete");
        return;
    }
    
}


public class Ex1 {

      public static void main(String[] args) {
        Thread ta = new Thread(new Ex1RunnerA());
        Thread tb = new Thread(new Ex1RunnerB());
        Thread tc = new Thread(new Ex1RunnerC());
        ta.start();
        tb.start();
        tc.start();
    }
    
    
}


运行结果

Constructing Ex1RunnerA
Constructing Ex1RunnerB
Constructing Ex1RunnerC
Hi from Ex1RunnerB
Hi from Ex1RunnerB
Hi from Ex1RunnerB
Ex1RunnerB task complete
Hi from Ex1RunnerA
Hi from Ex1RunnerC
Hi from Ex1RunnerA
Hi from Ex1RunnerA
Ex1RunnerA task complete
Hi from Ex1RunnerC
Hi from Ex1RunnerC
Ex1RunnerC task complete

9创建一个任务,它可以产生由n个斐波那契数字组成的序列,其中n是由任务的构造器而提供的。使用线程创建大量的这种任务并驱动它们。

package com.lhsjohn.comcurrency.prac;


class Ex2FibonacciA implements Runnable{
  
    private int n = 0;
    public Ex2FibonacciA(int n) {
       this.n = n;
    }
    
    public int fib(int x) {
        if(x<2) return 1;
        return fib(x-2) + fib(x-1);
    }

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

class Ex2FibonacciB implements Runnable{
      
    private int n = 0;
    public Ex2FibonacciB(int n) {
       this.n = n;
    }
    
    public int fib(int x) {
        if(x<2) return 1;
        return fib(x-2) + fib(x-1);
    }

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

class Ex2FibonacciC implements Runnable{
      
    private int n = 0;
    public Ex2FibonacciC(int n) {
       this.n = n;
    }
    
    public int fib(int x) {
        if(x<2) return 1;
        return fib(x-2) + fib(x-1);
    }

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

class Ex2FibonacciD implements Runnable{
      
    private int n = 0;
    public Ex2FibonacciD(int n) {
       this.n = n;
    }
    
    public int fib(int x) {
        if(x<2) return 1;
        return fib(x-2) + fib(x-1);
    }

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


public class Ex2 {
     
    public static void main(String[] args) {
        Thread f1 = new Thread(new Ex2FibonacciA(15));
        Thread f2 = new Thread(new Ex2FibonacciB(15));
        Thread f3 = new Thread(new Ex2FibonacciC(15));
        Thread f4 = new Thread(new Ex2FibonacciD(15));
        f1.start();
        f2.start();
        f3.start();
        f4.start();
    }
}


输出结果:


1123581321341121123581321341123581321345555891442333773610558958132134558989144233144233144233377377377610610610

10.使用不同类型的执行器重复练习8


package com.lhsjohn.comcurrency.prac;

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

class Ex3RunnerA implements Runnable{

    public Ex3RunnerA() {
      System.out.println("Constructing Ex3RunnerA...");
    }
    
    @Override
    public void run() {
        for(int i = 0; i<3;i++) {
           System.out.println("Hi Ex3RunnerA!");
           Thread.yield();
        }
      return;
    }
    
}

class Ex3RunnerB implements Runnable{

    public Ex3RunnerB() {
      System.out.println("Constructing Ex3RunnerB...");
    }
    
    @Override
    public void run() {
        for(int i = 0; i<3;i++) {
           System.out.println("Hi Ex3RunnerB!");
           Thread.yield();
        }
      return;
    }
    
}

class Ex3RunnerC implements Runnable{

    public Ex3RunnerC() {
      System.out.println("Constructing Ex3RunnerC...");
    }
    
    @Override
    public void run() {
        for(int i = 0; i<3;i++) {
           System.out.println("Hi Ex3RunnerC!");
           Thread.yield();
        }
        return;
    }
}

public class Ex3 {
    
    public static void main(String[] args) {
        ExecutorService exec1 = Executors.newCachedThreadPool();
        exec1.execute(new Ex3RunnerA());
        exec1.execute(new Ex3RunnerB());
        exec1.execute(new Ex3RunnerC());
        exec1.shutdown();
        
        ExecutorService exec2 = Executors.newFixedThreadPool(3);
        exec2.execute(new Ex3RunnerA());
        exec2.execute(new Ex3RunnerB());
        exec2.execute(new Ex3RunnerC());
        exec2.shutdown();
        
        ExecutorService exec3 = Executors.newSingleThreadExecutor();
        exec3.execute(new Ex3RunnerA());
        exec3.execute(new Ex3RunnerB());
        exec3.execute(new Ex3RunnerC());
        exec3.shutdown();

    }
}



输出结果:


Constructing Ex3RunnerA...
Hi Ex3RunnerA!
Hi Ex3RunnerA!
Hi Ex3RunnerA!
Constructing Ex3RunnerB...
Hi Ex3RunnerB!
Hi Ex3RunnerB!
Hi Ex3RunnerB!
Constructing Ex3RunnerC...
Hi Ex3RunnerC!
Hi Ex3RunnerC!
Hi Ex3RunnerC!
Constructing Ex3RunnerA...
Constructing Ex3RunnerB...
Hi Ex3RunnerA!
Hi Ex3RunnerA!
Hi Ex3RunnerA!
Constructing Ex3RunnerC...
Hi Ex3RunnerB!
Hi Ex3RunnerB!
Hi Ex3RunnerB!
Hi Ex3RunnerC!
Hi Ex3RunnerC!
Hi Ex3RunnerC!
Constructing Ex3RunnerA...
Constructing Ex3RunnerB...
Constructing Ex3RunnerC...
Hi Ex3RunnerA!
Hi Ex3RunnerA!
Hi Ex3RunnerA!
Hi Ex3RunnerB!
Hi Ex3RunnerB!
Hi Ex3RunnerB!
Hi Ex3RunnerC!
Hi Ex3RunnerC!
Hi Ex3RunnerC!


11.使用不同类型的执行器重复练习9


package com.lhsjohn.comcurrency.prac;

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

class Ex4FibonacciA implements Runnable{
    private int n = 0;
    public Ex4FibonacciA(int n) {
        this.n = n ;
    }
    
    public int fib(int x) {
        if(x<2)  return 1;
        return fib(x-2) + fib(x-1);
    }
    
    @Override
    public void run() {
        for(int i=0;i<n;i++) {
            System.out.println(fib(i));
        }
    }
    
}

class Ex4FibonacciB implements Runnable{
    private int n = 0;
    
    public Ex4FibonacciB(int n) {
        this.n = n ;
    }
    
    public int fib(int x) {
        if(x<2)  return 1;
        return fib(x-2) + fib(x-1);
    }
    
    @Override
    public void run() {
        for(int i=0;i<n;i++) {
            System.out.println(fib(i));
        }
    }
    
}

class Ex4FibonacciC implements Runnable{
    private int n = 0;
    
    public Ex4FibonacciC(int n) {
        this.n = n ;
    }
    
    public int fib(int x) {
        if(x<2)  return 1;
        return fib(x-2) + fib(x-1);
    }
    
    @Override
    public void run() {
        for(int i=0;i<n;i++) {
            System.out.println(fib(i));
        }
    }
    
}

class Ex4FibonacciD implements Runnable{
    private int n = 0;
    
    public Ex4FibonacciD(int n) {
        this.n = n ;
    }
    
    public int fib(int x) {
        if(x<2)  return 1;
        return fib(x-2) + fib(x-1);
    }
    
    @Override
    public void run() {
        for(int i=0;i<n;i++) {
            System.out.println(fib(i));
        }
    }
    
}


public class Ex4 {
    public static void main(String[] args) {
        ExecutorService exec1 = Executors.newCachedThreadPool();
        exec1.execute(new Ex4FibonacciA(15));
        exec1.execute(new Ex4FibonacciB(15));
        exec1.execute(new Ex4FibonacciC(15));
        exec1.execute(new Ex4FibonacciD(15));
        exec1.shutdown();
        
        
        
        ExecutorService exec2 = Executors.newFixedThreadPool(4);
        exec2.execute(new Ex4FibonacciA(15));
        exec2.execute(new Ex4FibonacciB(15));
        exec2.execute(new Ex4FibonacciC(15));
        exec2.execute(new Ex4FibonacciD(15));
        exec2.shutdown();
        
        
        ExecutorService exec3 = Executors.newSingleThreadExecutor();
        exec3.execute(new Ex4FibonacciA(15));
        exec3.execute(new Ex4FibonacciB(15));
        exec3.execute(new Ex4FibonacciC(15));
        exec3.execute(new Ex4FibonacciD(15));
        exec3.shutdown();
    
        
    }
    
    
    
}



输出结果:略 见上面

作者: lhsjohn

上一篇下一篇

猜你喜欢

热点阅读