java编程思想摘录和代码实验
(一)。线程基础一。有两种方法创建新的线程。
第一种
定义线程类实现Runnable接口
Thread myThread = new Thead(target) //target为Runnable接口类型。
Runnable中只有一个方法:
public void run();用以定义线程运行体。
使用Runnable接口可以为多个线程提供共享的数据。
在实现Runnable接口的类的run方法定义中可以使用Thread的静态方法。
public static Thread currentThread() 获取当前线程的引用
第二种
可以定义一个Thread的子类并重写run方法如下:
class MyThread extends Thead {
public void run(){...}
}
然后生成该类的对象:
MyThread myThread = new MyThread(...)
例子:
package com.thread.test;
public class TestThread1 {
public static void main(String[] args) {
Runner1 r = new Runner1();
Thread t = new Thread(r);
t.start();
for(int i = 0; i < 20; i++) {
System.out.println("Main Thread:---------" + i);
}
}
}
class Runner1 implements Runnable {
public void run() {
for(int i = 0; i <20; i++) {
System.out.println("Runner1: " + i);
}
}
}
或者:
package com.thread.test;
public class TestThread11 {
public static void main(String[] args) {
Runner11 ru = new Runner11();
ru.start();
for(int i = 0; i < 100; i++) {
System.out.println("main--------------" + i);
}
}
}
class Runner11 extends Thread{
public void run() {
for(int i = 0; i < 100; i++) {
System.out.println("run:" + i);
}
}
}
推荐使用接口,因为实现接口的时候还可以继承其他类。
第二种方法 ,只能继承一个类,所以不推荐。
------------------------------------------------------------------------------------------------------------
(二)sleep方法
方法:
isAlive()-判断线程是否还“活”着,即线程是否还未终止
getPriority()-获得优先级数值
setPriority()
Thread.sleep()-将当前线程睡眠指定毫秒数
join()-调用某线程的该方法,将当前线程与该线程“合并”,即等待该线程结束,再恢复当前线程的运行。
yield()-让出CPU,当前线程进入就绪队列等待调度。
wait()-当前线程进入对象的wait pool
notify()-唤醒对象的wait pool中的一个
notifyAll()-唤醒对象的wait pool中所有等待线程
---------------------------------
sleep(静态方法)/join/yield方法
sleep方法:
可以调用Thread的静态方法,public static void sleep(long millis)throws InterruptedException
使得当前线程休眠(暂时停止执行millis毫秒)
由于是静态方法,sleep可以由类名直接调用:Thread.sleep()
join方法:
合并某个线程
yield方法:
让出CPU,给其他线程执行的机会
package com.thread.test;
import java.util.Date;
/**
* interrupt
public void interrupt()中断线程。
如果当前线程没有中断它自己(这在任何情况下都是允许的),则该线程的 checkAccess 方法就会被调用,这可能抛出 SecurityException。
如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。
如果该线程在可中断的通道上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个 ClosedByInterruptException。
如果该线程在一个 Selector 中受阻,则该线程的中断状态将被设置,它将立即从选择操作返回,并可能带有一个非零值,就好像调用了选择器的 wakeup 方法一样。
如果以前的条件都没有保存,则该线程的中断状态将被设置。
中断一个不处于活动状态的线程不需要任何作用。
抛出:
SecurityException - 如果当前线程无法修改该线程
* */
public class TestInterrupt {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
try{
Thread.sleep(10000);//main线程睡眠
}catch(InterruptedException e) {}
thread.interrupt();//中断线程。(同时可以让子线程结束)
}
}
class MyThread extends Thread {
public void run() {
while(true) {
System.out.println("===" + new Date() + "===");
try {
sleep(1000);
}catch(InterruptedException e) {
System.out.println("aaaaaa");
return ;
}
}
}
}
输出:
===Tue Nov 18 10:03:19 CST 2014===
===Tue Nov 18 10:03:20 CST 2014===
===Tue Nov 18 10:03:21 CST 2014===
===Tue Nov 18 10:03:22 CST 2014===
===Tue Nov 18 10:03:23 CST 2014===
===Tue Nov 18 10:03:24 CST 2014===
===Tue Nov 18 10:03:25 CST 2014===
===Tue Nov 18 10:03:26 CST 2014===
===Tue Nov 18 10:03:27 CST 2014===
===Tue Nov 18 10:03:28 CST 2014===
aaaaaa
-----------------------------------------------------------------------------------------------------