[size=medium]以下是一些java线程的小李子,欢迎大家讨论[/size] :D
[b]第一个例子:[/b]线程的interrupt方法的使用
[size=medium]执行结果:
Starting thread...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread...
Thread is running...
Thread is running...
Thread is running...
Stopping application...[/size]
[b]第二个例子: [/b] stop
执行结果;
Starting thread...
Thread is running...
Thread is running...
Thread is running...
Asking thread to stop...
Thread exiting under request...
Stopping application...
[b]第三个例子:[/b] [size=medium]正确的停止线程方式是设置共享变量,并调用interrupt()(注意变量应该先设置)。如果线程没有被阻塞,
这时调用interrupt()将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。
在任何一种情况中,最后线程都将检查共享变量然后再停止。Listing C这个示例描述了该技术。[/size]
[size=medium]Starting thread...
Thread running...
Thread running...
Thread running...
Asking thread to stop...
Thread interrupted...
Thread exiting under request...
Stopping application...
第四个例子[/size]:
[b]第五个例子:wait的使用[/b]
[size=medium]出现异常:
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
waiting
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at multithread.DSRoss.notwait(Agg.java:31)
at multithread.DSRoss.run(Agg.java:23)[/size]
join的使用:
[size=medium]当没有t.join();时,主线程和线程的执行顺序是不一定的,但有了t.join();
之后,会保证t先执行完在执行main线程
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
false
lala[/size]
[b]第一个例子:[/b]线程的interrupt方法的使用
package threadtest;
public class Thread1 extends Thread {
private boolean stop = false;
public static void main(String args[]) throws Exception {
Thread1 thread = new Thread1();
System.out.println("Starting thread...");
thread.start();
Thread.sleep(3000);
System.out.println("Interrupting thread..."); // 这种方法并不会中断一个正在运行的线程
thread.interrupt();
Thread.sleep(3000);
System.out.println("Stopping application...");
System.exit(0);
}
public void run() {
while (!stop) {
System.out.println("Thread is running...");
long time = System.currentTimeMillis();
while ((System.currentTimeMillis() - time < 1000) && (!stop)) {
}
}
}
}
[size=medium]执行结果:
Starting thread...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread...
Thread is running...
Thread is running...
Thread is running...
Stopping application...[/size]
[b]第二个例子: [/b] stop
package threadtest;
public class Thread2 extends Thread {
volatile boolean stop = false;
public static void main( String args[] ) throws Exception {
Thread2 thread = new Thread2();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Asking thread to stop..." );
thread.stop = true; //真的停止了
Thread.sleep( 3000 );
System.out.println( "Stopping application..." );
System.exit( 0 );
}
public void run() {
while ( !stop ) {
System.out.println( "Thread is running..." );
long time = System.currentTimeMillis();
while ( (System.currentTimeMillis()-time < 1000) && (!stop) ) {
}
}
System.out.println( "Thread exiting under request..." );
}
}
执行结果;
Starting thread...
Thread is running...
Thread is running...
Thread is running...
Asking thread to stop...
Thread exiting under request...
Stopping application...
[b]第三个例子:[/b] [size=medium]正确的停止线程方式是设置共享变量,并调用interrupt()(注意变量应该先设置)。如果线程没有被阻塞,
这时调用interrupt()将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。
在任何一种情况中,最后线程都将检查共享变量然后再停止。Listing C这个示例描述了该技术。[/size]
package threadtest;
public class Thread3 extends Thread {
volatile boolean stop = false;
public static void main( String args[] ) throws Exception {
Thread3 thread = new Thread3();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Asking thread to stop..." );
thread.stop = true;
thread.interrupt(); //停止了再阻断会有异常发生
Thread.sleep( 3000 );
System.out.println( "Stopping application..." );
System.exit( 0 );
}
public void run() {
while ( !stop ) {
System.out.println( "Thread running..." );
try {
Thread.sleep( 1000 );
} catch ( InterruptedException e ) {
System.out.println( "Thread interrupted..." );
}
}
System.out.println( "Thread exiting under request..." );
}
}
[size=medium]Starting thread...
Thread running...
Thread running...
Thread running...
Asking thread to stop...
Thread interrupted...
Thread exiting under request...
Stopping application...
第四个例子[/size]:
package multithread;
public class Test extends Thread{
public static void main(String argv[])
{
Test b = new Test();
b.run();
}
public void start() //start方法才是启动线程的方法,一般不需要重写start方法,重写run方法即可
{
for (int i = 0; i <10; i++){
System.out.println("Value of i = " + i);
}
}
}
执行结果,没有任何输出,因为线程只有调用start方法才进入运行状态
[b]第五个例子:wait的使用[/b]
package multithread;
public class Agg {
public static void main(String argv[]) {
Agg a = new Agg();
a.go();
}
public void go() {
DSRoss ds1 = new DSRoss("one");
ds1.start();
}
}
class DSRoss extends Thread {
private String sTname = "";
DSRoss(String s) {
sTname = s;
}
public void run() {
notwait();
System.out.println("finished");
}
public void notwait() {
while (true) {
try {
System.out.println("waiting");
wait();
} catch (InterruptedException ie) {
}
System.out.println(sTname);
notifyAll();
}
}
}
[size=medium]出现异常:
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
waiting
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at multithread.DSRoss.notwait(Agg.java:31)
at multithread.DSRoss.run(Agg.java:23)[/size]
join的使用:
class MyThread extends Thread{
public void run(){
for(int i=0;i<20;i++){
System.out.println("lala"+this.getName());
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
// t.join();
System.out.println(Double.NaN>5);
System.out.println("lala");
}
}
[size=medium]当没有t.join();时,主线程和线程的执行顺序是不一定的,但有了t.join();
之后,会保证t先执行完在执行main线程
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
lalaThread-0
false
lala[/size]