标签:active 改变 生成 操作 知识点 new false local length
1、继承Thread类,并且覆盖run()方法。
2、实现Runnable接口类,使用带参数的Thread构造器来创建Thread对象,参数就是Runnable接口的一个对象。
那么创建完了怎么运行呢?调用run()方法?调用run()方法只是简单的 类对象调用自己的成员方法,那么怎么会开启线程呢?而且每一个线程还有自己的信息(线程名字,线程ID,优先级等)。那么应该是怎么运行呢?答案是调用start()方法。1 //创建 2 public class Thread2 extends Thread{ 3 @Override 4 public void run() { 5 System.out.println("继承Thread类创建线程"); 6 } 7 } 8 //运行 9 private static void createThread2() { 10 Thread2 t2 = new Thread2(); 11 Thread thread = new Thread(t2); 12 thread.start(); 13 }
1 //创建 2 public class Thread1 implements Runnable { 3 @Override 4 public void run() { 5 System.out.println("实现Runnable接口创建线程"); 6 } 7 } 8 //运行 9 private static void createThread1() { 10 Runnable t1 = new Thread1(); 11 Thread thread = new Thread(t1); 12 thread.start(); 13 }
1 public enum State { 2 NEW, 3 RUNNABLE, 4 BLOCKED, 5 WAITING, 6 TIMED_WAITING, 7 TERMINATED; 8 }
1 task.interrupt();
1 public static boolean interrupted() { 2 return currentThread().isInterrupted(true); 3 }
1 @Override 2 public void run() { 3 File file = new File(initPath); 4 if (file.isDirectory()) { 5 try { 6 dirctoryProcess(file); 7 } catch (InterruptedException e) { 8 System.out.printf("%s:the search has been interrupted", Thread.currentThread().getName()); 9 } 10 } 11 }
1 private void dirctoryProcess(File file) throws InterruptedException { 2 File[] list = file.listFiles(); 3 if (list != null) { 4 for (int i = 0; i < list.length; i++) { 5 if (list[i].isDirectory()) { 6 dirctoryProcess(list[i]); 7 } else { 8 fileProcess(list[i]); 9 } 10 } 11 } 12 if (Thread.interrupted()) { 13 throw new InterruptedException(); 14 } 15 }
1 public class DataSourceLoader implements Runnable { 2 @Override 3 public void run() { 4 System.out.printf("Begining data sources loading: %s\n",new Date()); 5 try { 6 TimeUnit.SECONDS.sleep(3); 7 } catch (InterruptedException e) { 8 e.printStackTrace(); 9 } 10 System.out.printf("Data Sources loading has finised:%s\n",new Date()); 11 } 12 }
1 public class NetWorkConnectionLoader implements Runnable { 2 @Override 3 public void run() { 4 System.out.printf("Begining data sources loading: %s\n",new Date()); 5 try { 6 TimeUnit.SECONDS.sleep(6); 7 } catch (InterruptedException e) { 8 e.printStackTrace(); 9 } 10 System.out.printf("Data Sources loading has finised:%s\n",new Date()); 11 } 12 }
1 public class Main { 2 public static void main(String[] args) { 3 DataSourceLoader dsLoader = new DataSourceLoader(); 4 Thread t1 = new Thread(dsLoader,"DataSourceThread"); 5 NetWorkConnectionLoader ncLoader = new NetWorkConnectionLoader(); 6 Thread t2 = new Thread(ncLoader,"NetWorkConnectionThread"); 7 t1.start(); 8 t2.start(); 9 try { 10 t1.join(); 11 t2.join(); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 System.out.printf("Main: Configuration has been loaded:%s\n",new Date()); 16 } 17 }
1 Begining data sources loading: Tue Apr 25 14:46:39 CST 2017 2 Begining data sources loading: Tue Apr 25 14:46:39 CST 2017 3 Data Sources loading has finised:Tue Apr 25 14:46:42 CST 2017 4 NetWorkConnectionLoader loading has finised:Tue Apr 25 14:46:45 CST 2017 5 Main: Configuration has been loaded:Tue Apr 25 14:46:45 CST 2017
Main: Configuration has been loaded:Tue Apr 25 14:46:45 CST 2017
1 public final void join() throws InterruptedException { 2 join(0); 3 }
1 public final synchronized void join(long millis) 2 throws InterruptedException { 3 long base = System.currentTimeMillis(); 4 long now = 0; 5 if (millis < 0) { 6 throw new IllegalArgumentException("timeout value is negative"); 7 } 8 if (millis == 0) { 9 while (isAlive()) { 10 wait(0); 11 } 12 } else { 13 while (isAlive()) { 14 long delay = millis - now; 15 if (delay <= 0) { 16 break; 17 } 18 wait(delay); 19 now = System.currentTimeMillis() - base; 20 } 21 } 22 }
1 public class WriterTask implements Runnable { 2 Deque<Event> deque; 3 4 public WriterTask (Deque<Event> deque){ 5 this.deque=deque; 6 } 7 8 @Override 9 public void run() { 10 11 // Writes 100 events 12 for (int i=1; i<100; i++) { 13 // Creates and initializes the Event objects 14 Event event=new Event(); 15 event.setDate(new Date()); 16 event.setEvent(String.format("The thread %s has generated an event",Thread.currentThread().getId())); 17 18 // Add to the data structure 19 deque.addFirst(event); 20 try { 21 // Sleeps during one second 22 TimeUnit.SECONDS.sleep(1); 23 } catch (InterruptedException e) { 24 e.printStackTrace(); 25 } 26 } 27 } 28 }
1 public class Event { 2 3 private Date date; 4 private String event; 5 6 public Date getDate() { 7 return date; 8 } 9 10 11 public void setDate(Date date) { 12 this.date = date; 13 } 14 15 16 public String getEvent() { 17 return event; 18 } 19 20 public void setEvent(String event) { 21 this.event = event; 22 } 23 }
1 public class CleanerTask extends Thread { 2 private Deque<Event> deque; 3 4 public CleanerTask(Deque<Event> deque) { 5 this.deque = deque; 6 // Establish that this is a Daemon Thread 7 setDaemon(true); 8 } 9 10 @Override 11 public void run() { 12 while (true) { 13 Date date = new Date(); 14 clean(date); 15 } 16 } 17 18 private void clean(Date date) { 19 long difference; 20 boolean delete; 21 22 if (deque.size()==0) { 23 return; 24 } 25 26 delete=false; 27 do { 28 Event e = deque.getLast(); 29 difference = date.getTime() - e.getDate().getTime(); 30 if (difference > 10000) { 31 System.out.printf("Cleaner: %s\n",e.getEvent()); 32 deque.removeLast(); 33 delete=true; 34 } 35 } while (difference > 10000); 36 if (delete){ 37 System.out.printf("Cleaner: Size of the queue: %d\n",deque.size()); 38 } 39 } 40 }
1 public static void main(String[] args) { 2 3 Deque<Event> deque=new ArrayDeque<Event>(); 4 5 WriterTask writer=new WriterTask(deque); 6 for (int i=0; i<3; i++){ 7 Thread thread=new Thread(writer); 8 thread.start(); 9 } 10 11 CleanerTask cleaner=new CleanerTask(deque); 12 cleaner.start(); 13 }
1 public class ExceptionHandler implements UncaughtExceptionHandler { 2 @Override 3 public void uncaughtException(Thread t, Throwable e) { 4 System.out.printf("An exception has been captured\n"); 5 System.out.printf("Thread: %s\n",t.getId()); 6 System.out.printf("Exception: %s: %s\n",e.getClass().getName(),e.getMessage()); 7 System.out.printf("Stack Trace: \n"); 8 e.printStackTrace(System.out); 9 System.out.printf("Thread status: %s\n",t.getState()); 10 } 11 }
1 @Override 2 public void run() { 3 // The next instruction always throws and exception 4 int numero=Integer.parseInt("TTT"); 5 }
1 public static void main(String[] args) { 2 Task task=new Task(); 3 Thread thread=new Thread(task); 4 thread.setUncaughtExceptionHandler(new ExceptionHandler()); 5 thread.start(); 6 try { 7 thread.join(); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 12 System.out.printf("Thread has finished\n"); 13 }
1 An exception has been captured 2 Thread: 10 3 Exception: java.lang.NumberFormatException: For input string: "TTT" 4 Stack Trace: 5 java.lang.NumberFormatException: For input string: "TTT" 6 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 7 at java.lang.Integer.parseInt(Integer.java:580) 8 at java.lang.Integer.parseInt(Integer.java:615) 9 at com.packtpub.java7.concurrency.chapter1.recipe8.task.Task.run(Task.java:16) 10 at java.lang.Thread.run(Thread.java:745) 11 Thread status: RUNNABLE 12 Thread has finished
1 Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: "TTT" 2 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 3 at java.lang.Integer.parseInt(Integer.java:580) 4 at java.lang.Integer.parseInt(Integer.java:615) 5 at com.packtpub.java7.concurrency.chapter1.recipe8.task.Task.run(Task.java:16) 6 at java.lang.Thread.run(Thread.java:745) 7 Thread has finished
1 public class SafeTask implements Runnable { 2 3 private static ThreadLocal<Date> startDate= new ThreadLocal<Date>() { 4 protected Date initialValue(){ 5 return new Date(); 6 } 7 }; 8 9 @Override 10 public void run() { 11 // Writes the start date 12 System.out.printf("Starting Thread: %s : %s\n",Thread.currentThread().getId(),startDate.get()); 13 try { 14 TimeUnit.SECONDS.sleep((int)Math.rint(Math.random()*10)); 15 } catch (InterruptedException e) { 16 e.printStackTrace(); 17 } 18 // Writes the start date 19 System.out.printf("Thread Finished: %s : %s\n",Thread.currentThread().getId(),startDate.get()); 20 } 21 }
1 ThreadGroup threadGroup = new ThreadGroup("Searcher"); 2 SearchTask searchTask=new SearchTask(result); 3 for (int i=0; i<5; i++) { 4 Thread thread=new Thread(threadGroup, searchTask); 5 thread.start(); 6 try { 7 TimeUnit.SECONDS.sleep(1); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 }
1 private static void waitFinish(ThreadGroup threadGroup) { 2 while (threadGroup.activeCount()>9) { 3 try { 4 TimeUnit.SECONDS.sleep(1); 5 } catch (InterruptedException e) { 6 e.printStackTrace(); 7 } 8 } 9 }
1 threadGroup.interrupt();
1 public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) { 2 checkAccess(); 3 uncaughtExceptionHandler = eh; 4 }
1 class ThreadGroup implements Thread.UncaughtExceptionHandler {
1 public class MyThreadGroup extends ThreadGroup { 2 public MyThreadGroup(String name) { 3 super(name); 4 } 5 @Override 6 public void uncaughtException(Thread t, Throwable e) { 7 } 8 }
1 public interface ThreadFactory { 2 Thread newThread(Runnable r); 3 }
1 public class Thread1 implements Runnable{ 2 @override 3 public void run(){ 4 } 5 } 6 Runnable t1 = new Thread1(); 7 Thread t = new Thread(t1); 8 t.start();
1 public class MyThreadFactory implements ThreadFactory { 2 @Override 3 public Thread newThread(Runnable r) { 4 Thread t=new Thread(r,"thread_name"); 5 return t; 6 } 7 }
1 Task task = new Task(); 2 Thread thread=factory.newThread(task);
标签:active 改变 生成 操作 知识点 new false local length
原文地址:http://www.cnblogs.com/uodut/p/6775217.html