本文共 5780 字,大约阅读时间需要 19 分钟。
public class StoppableTask extends Thread { private volatile boolean pleaseStop; public void run() { while (!pleaseStop) { // do some stuff... } } public void tellMeToStop() { pleaseStop = true; } } public class StoppableTask extends Thread { private volatile boolean pleaseStop; public void run() { while (!pleaseStop) { // do some stuff... } } public void tellMeToStop() { pleaseStop = true; } } |
int i1; int geti1() {return i1;} volatile int i2; int geti2() {return i2;} int i3; synchronized int geti3() {return i3;} geti1() int i1; int geti1() {return i1;} volatile int i2; int geti2() {return i2;} int i3; synchronized int geti3() {return i3;} geti1() |
package mythread; public class JoinThread extends Thread { public static volatile int n = 0 ; public void run() { for ( int i = 0 ; i < 10 ; i ++ ) try { n = n + 1 ; sleep( 3 ); // 为了使运行结果更随机,延迟3毫秒 } catch (Exception e) { } } public static void main(String[] args) throws Exception { Thread threads[] = new Thread[ 100 ]; for ( int i = 0 ; i < threads.length; i ++ ) // 建立100个线程 threads[i] = new JoinThread(); for ( int i = 0 ; i < threads.length; i ++ ) // 运行刚才建立的100个线程 threads[i].start(); for ( int i = 0 ; i < threads.length; i ++ ) // 100个线程都执行完后继续 threads[i].join(); System.out.println( " n= " + JoinThread.n); } } package mythread; public class JoinThread extends Thread { public static volatile int n = 0 ; public void run() { for ( int i = 0 ; i < 10 ; i ++ ) try { n = n + 1 ; sleep( 3 ); // 为了使运行结果更随机,延迟3毫秒 } catch (Exception e) { } } public static void main(String[] args) throws Exception { Thread threads[] = new Thread[ 100 ]; for ( int i = 0 ; i < threads.length; i ++ ) // 建立100个线程 threads[i] = new JoinThread(); for ( int i = 0 ; i < threads.length; i ++ ) // 运行刚才建立的100个线程 threads[i].start(); for ( int i = 0 ; i < threads.length; i ++ ) // 100个线程都执行完后继续 threads[i].join(); System.out.println( " n= " + JoinThread.n); } } |
package mythread; public class JoinThread extends Thread { public static int n = 0 ; public static synchronized void inc() { n ++ ; } public void run() { for ( int i = 0 ; i < 10 ; i ++ ) try { inc(); // n = n + 1 改成了 inc(); sleep( 3 ); // 为了使运行结果更随机,延迟3毫秒 } catch (Exception e) { } } public static void main(String[] args) throws Exception { Thread threads[] = new Thread[ 100 ]; for ( int i = 0 ; i < threads.length; i ++ ) // 建立100个线程 threads[i] = new JoinThread(); for ( int i = 0 ; i < threads.length; i ++ ) // 运行刚才建立的100个线程 threads[i].start(); for ( int i = 0 ; i < threads.length; i ++ ) // 100个线程都执行完后继续 threads[i].join(); System.out.println( " n= " + JoinThread.n); } } package mythread; public class JoinThread extends Thread { public static int n = 0 ; public static synchronized void inc() { n ++ ; } public void run() { for ( int i = 0 ; i < 10 ; i ++ ) try { inc(); // n = n + 1 改成了 inc(); sleep( 3 ); // 为了使运行结果更随机,延迟3毫秒 } catch (Exception e) { } } public static void main(String[] args) throws Exception { Thread threads[] = new Thread[ 100 ]; for ( int i = 0 ; i < threads.length; i ++ ) // 建立100个线程 threads[i] = new JoinThread(); for ( int i = 0 ; i < threads.length; i ++ ) // 运行刚才建立的100个线程 threads[i].start(); for ( int i = 0 ; i < threads.length; i ++ ) // 100个线程都执行完后继续 threads[i].join(); System.out.println( " n= " + JoinThread.n); } } |
转载地址:http://vdsva.baihongyu.com/