Java程序设计实用教程 by 朱战立 & 沈伟
孙鑫Java无难事
Java 多线程与并发编程专题(http://www.ibm.com/developerworks/cn/java/j-concurrent/)
Java 线程简介(http://www.ibm.com/developerworks/cn/education/java/j-threads/j-threads.html)
http://www.cnblogs.com/liuling/p/2013-9-13-01.html
书是入门级的,后面的专题是出自业界的技术文章。
1 public class TestSimpleThread extends Thread { 2 public TestSimpleThread(String str) 3 { 4 super(str); 5 } 6 7 public void run() 8 { 9 for (int i = 0; i < 5; i ++) 10 { 11 System.out.println(i + " " + getName()); 12 try 13 { 14 sleep((long)(Math.random() * 1000)); 15 } catch (InterruptedException e) {} 16 } 17 System.out.println(getName() + " Finish!"); 18 } 19 20 public static void main(String[] args) 21 { 22 new TestSimpleThread("Java").start(); 23 new TestSimpleThread("C++").start(); 24 } 25 }
1 public class TestSimpleRunnable implements Runnable 2 { 3 private String str; 4 private Thread myThread; 5 6 public TestSimpleRunnable(String str) 7 { 8 this.str = str; 9 } 10 11 public void myStart() 12 { 13 myThread = new Thread(this, str); 14 myThread.start(); 15 } 16 17 public void run() 18 { 19 for (int i = 0; i < 5; i ++) 20 { 21 System.out.println(i + " " + myThread.getName()); 22 try 23 { 24 Thread.sleep((long)(Math.random() * 1000)); 25 } catch (InterruptedException e) {} 26 } 27 System.out.println(myThread.getName() + " Finished!"); 28 } 29 30 public static void main (String[] args) 31 { 32 new TestSimpleRunnable("Java").myStart(); 33 new TestSimpleRunnable("C++").myStart(); 34 } 35 }
1 public class EnumerateTest extends Thread 2 { 3 public void listCurrentThreads() 4 { 5 ThreadGroup currentGroup = Thread.currentThread().getThreadGroup(); 6 7 int numThreads = currentGroup.activeCount(); 8 System.out.println("numThreads = " + numThreads); 9 Thread[] listOfThreads = new Thread[numThreads - 1]; 10 11 currentGroup.enumerate(listOfThreads); 12 13 for (int i = 0; i < numThreads - 1; i ++) 14 { 15 System.out.println("Thread #" + i + " = " + listOfThreads[i].getName()); 16 } 17 } 18 19 public static void main (String[] args) 20 { 21 EnumerateTest a = new EnumerateTest(); 22 EnumerateTest b = new EnumerateTest(); 23 a.start(); 24 b.start(); 25 a.listCurrentThreads(); 26 } 27 }
1 public class TestThreadPrio extends Thread 2 { 3 private String name; 4 5 public TestThreadPrio (String name) 6 { 7 this.name = name; 8 } 9 10 public void run() 11 { 12 for (int i = 0; i < 2; i ++) 13 { 14 System.out.println(name + " " + getPriority()); 15 try 16 { 17 Thread.sleep((int)(Math.random() * 100)); 18 } 19 catch(InterruptedException e) {} 20 } 21 } 22 23 public static void main (String args[]) 24 { 25 Thread t1 = new TestThreadPrio("Thread1"); 26 t1.setPriority(Thread.MIN_PRIORITY); 27 Thread t2 = new TestThreadPrio("Thread2"); 28 t2.setPriority(3); 29 Thread t3 = new TestThreadPrio("Thread3"); 30 t3.setPriority(Thread.NORM_PRIORITY); 31 Thread t4 = new TestThreadPrio("Thread4"); 32 t4.setPriority(7); 33 Thread t5 = new TestThreadPrio("Thread5"); 34 t5.setPriority(Thread.MAX_PRIORITY); 35 36 t1.start(); 37 t2.start(); 38 t3.start(); 39 t4.start(); 40 t5.start(); 41 } 42 }
1 public class Queue 2 { 3 private int count; 4 private int front; 5 private int rear; 6 private char[] dat = new char[10]; 7 8 public Queue() 9 { 10 count = 0; 11 front = 0; 12 rear = 0; 13 } 14 15 public void push(char c) 16 { 17 if (count >= 10) 18 { 19 System.out.println("队列已满"); 20 return; 21 } 22 dat[rear] = c; 23 System.out.println("插入的字符: " + c); 24 rear ++; 25 count ++; 26 } 27 28 public synchronized char pop() 29 { 30 if (count <= 0) 31 { 32 System.out.println("队列已空"); 33 return ' '; 34 } 35 char temp = dat[front]; 36 System.out.println("删除的字符: " + temp); 37 try 38 { 39 Thread.sleep((int)(Math.random() * 100)); 40 } catch (InterruptedException e) {} 41 front ++; 42 count --; 43 return temp; 44 } 45 }
1 public class Consumer implements Runnable 2 { 3 private Queue qu; 4 5 public Consumer(Queue s) 6 { 7 qu = s; 8 } 9 10 public void run() 11 { 12 for (int i = 0; i < 3; i ++) 13 { 14 qu.pop(); 15 } 16 } 17 }
1 public class TestQueue 2 { 3 public static void main(String args[]) 4 { 5 Queue qu = new Queue(); 6 for (char c = 'a'; c <= 'd'; c ++) 7 qu.push(c); 8 Runnable sink = new Consumer(qu); 9 Thread t1 = new Thread(sink); 10 Thread t2 = new Thread(sink); 11 t1.start(); 12 t2.start(); 13 } 14 }
1 public class Storage 2 { 3 private int goods; 4 private boolean available; 5 6 public Storage(int g) 7 { 8 goods = g; 9 available = false; 10 } 11 12 public synchronized void put(int g) 13 { 14 while (available == true) 15 { 16 try 17 { 18 wait(); 19 } catch (InterruptedException e) {} 20 } 21 available = true; 22 23 goods = g; 24 System.out.println("put goods = " + goods); 25 notify(); 26 } 27 28 public synchronized int get() 29 { 30 while (available == false) 31 { 32 try 33 { 34 wait(); 35 } catch (InterruptedException e) {} 36 } 37 available = false; 38 39 int temp = goods; 40 System.out.println("get goods = " + goods); 41 goods = 0; 42 notify(); 43 return temp; 44 } 45 }
1 public class Producer extends Thread 2 { 3 private Storage tb; 4 5 public Producer(Storage c) 6 { 7 tb = c; 8 } 9 10 public void run() 11 { 12 for (int i = 11; i < 16; i ++) 13 { 14 tb.put(i); 15 } 16 } 17 }
1 public class Consumer extends Thread 2 { 3 private Storage tb; 4 5 public Consumer(Storage c) 6 { 7 tb = c; 8 } 9 10 public void run() 11 { 12 int g; 13 for (int i = 11; i < 16; i ++) 14 { 15 g = tb.get(); 16 } 17 } 18 }
1 public class TestStorage 2 { 3 public static void main(String[] args) 4 { 5 Storage com = new Storage(0); 6 7 Producer p = new Producer(com); 8 9 Consumer c = new Consumer(com); 10 11 p.start(); 12 c.start(); 13 } 14 }