Java线程学习笔记(一)

线程的创建与运行

public class Main {
   public static void main(String[] args){
        System.out.println("The main method was executed by thread:" + Thread.currentThread().getName());
        Helper helper = new Helper("Java Thread Anywhere");
        //创建一个线程
       Thread thread = new Thread(helper);
       //设置线程名
       thread.setName("A-Worker-Thread");
       //启动线程
       thread.start();
   }
    static class Helper implements Runnable {
        private final String message;
        public Helper(String message){
            this.message = message;
        }
        private void doSomething(String message){
            System.out.println("The doSomething method was executed by thread:" + Thread.currentThread().getName());
            System.out.println("Do something with " + message);
        }
        @Override
        public void run() {
            doSomething(message);
        }
    }
}

以线程的子类的方式创建线程

public class Main {
   public static void main(String[] args){
       Thread thread = new CustomThread();
       thread.start();
   }
   static class CustomThread extends Thread{

       @Override
       public void run() {
           System.out.println("Running...");
       }
   }
}

工作者线程

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Main {
   public static void main(String[] args){
        Helper helper = new Helper();
        helper.init();
        helper.submit("Something...");
   }
   static class Helper{
       private final BlockingQueue<String> workQueue = new ArrayBlockingQueue<String>(100);
       //用于处理队列workdQueue中的任务的工作者线程
       private final Thread workerThread = new Thread() {

           @Override
           public void run() {
               String task = null;
               while(true) {
                   try {
                       task = workQueue.take();//执行队列中的任务,然后移除任务
                   } catch (InterruptedException e) {
                       break;
                   }
                   System.out.println(doProcess(task));
               }
           }
       };
       public void init(){
           workerThread.start();
       }
       protected String doProcess(String task){
           return task + "->processed.";
       }
       public void submit(String task) {
           try {
               workQueue.put(task);
           } catch (InterruptedException e) {
               Thread.currentThread().interrupt();
           }
       }
   }
}

线程安全计数器

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Main {
   public static void main(String[] args){
       ThreadSafeCounter count = new ThreadSafeCounter();
       ThreadSafeCounter count1 = new ThreadSafeCounter();
       count1.start();
        count.start();
   }

   static class ThreadSafeCounter extends Thread{
       private int count = 0;
       public void increment(){
           synchronized (this){
               count++;
           }
       }
       public int get(){
           synchronized (this){
               return count;
           }
       }

       @Override
       public void run() {
           increment();
           System.out.println("count的值是:"+get());
       }
   }
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页