package com.fqc.concurrency;

import com.fqc.io.IOUtils;

/**
 * Created by fqc on 2016/1/4.
 */
public class SimpleTwoWayToBuildThread {

    public static void main(String[] args) {
        //Test1();
        Test2();


    }

    private static void Test2() {
        MyThread2 myThread1 = new MyThread2();
        MyThread2 myThread2 = new MyThread2();

        Thread t1 = new Thread(myThread1, "Thread-1");
        Thread t2 = new Thread(myThread2, "Thread-2");

        t1.start();
        t2.start();
    }


    private static void Test1() {
        //1,2
        MyThread myThread = new MyThread("Thread-1");
        MyThread myThread2 = new MyThread("Thread-2");


        myThread.start();
        myThread2.start();

        //以下方式是将两线程类当成是普通类来处理。
        //结果是 main-1-100 *2
        //切不可直接调用线程类的run方法，而是通过start方法来调用。
        myThread.run();
        myThread2.run();
    }

}

class MyThread extends Thread {


    private int a = 0; //1

    public MyThread(String name) {
        super(name);
    }

    public MyThread() {

    }

    @Override
    public void run() {
        //int a = 0; //2
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep((long) (Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(this.currentThread().getName() + "---" + a++);

        }
    }
}

class MyThread2 implements Runnable {
    private int a = 0; //1

    @Override
    public void run() {
        //int a = 0; //2
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep((long) (Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //System.out.println(this.currentThread().getName() + "---" + a++);
            System.out.println(Thread.currentThread().getName() + "---" + a++);

        }
    }
}
@@ -49,16 +49,224 @@
 IOUtils;
+%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0Aimport org.apache.log4j.BasicConfigurator;%0Aimport org.apache.log4j.ConsoleAppender;%0Aimport org.apache.log4j.PatternLayout;
 %0A%0A/**%0A *
@@ -336,24 +336,82 @@
 ldThread %7B%0A%0A
+    static %7B%0A        BasicConfigurator.configure();%0A    %7D%0A
     public s
@@ -479,18 +479,16 @@
 est2();%0A
-%0A%0A
     %7D%0A%0A 
@@ -1181,16 +1181,88 @@
 read %7B%0A%0A
+    public static final Log LOGGER = LogFactory.getLog(MyThread.class);%0A
 %0A    pri
@@ -1661,32 +1661,34 @@
   %7D%0A            
+//
 System.out.print
@@ -1741,65 +1741,269 @@
 +);%0A
-%0A        %7D%0A    %7D%0A%7D%0A%0Aclass MyThread2 implements Runnable %7B
+            LOGGER.info(this.currentThread().getName() + %22---%22 + a++);%0A%0A        %7D%0A    %7D%0A%7D%0A%0Aclass MyThread2 implements Runnable %7B%0A%0A    static %7B%0A        BasicConfigurator.configure();%0A    %7D%0A%0A%0A    public static final Log LOGGER = LogFactory.getLog(MyThread.class);
 %0A   
@@ -2399,16 +2399,18 @@
         
+//
 System.o
@@ -2460,32 +2460,105 @@
  + %22---%22 + a++);
+%0A            LOGGER.info(Thread.currentThread().getName() + %22---%22 + a++);
 %0A%0A        %7D%0A    

add commons logging and log4j