多线程(Multithreading)
Author : FoxMX
在现实生活中,我们经常使用多线程模型。我们在处理某些任务的同时也可以让孩子、配偶和父母完成别的任务。例如,我在写信的同时可能打发我的儿子去邮局买邮票。用软件术语来说,这称为多个控制(或执行)线程。线程是程序中的一个控制流程。
有两种生成线程的方法:
1.生成java.lang.Thread子类的新类并覆盖其run()方法。
2.声明一个实现java.lang.Runnable接口的类并将其传入Thread中运行。
Thread 类是一个具体的类,即不是抽象类,该类封装了线程的行为。要创建一个线程,程序员必须创建一个从 Thread 类导出的新类。程序员必须覆盖 Thread 的 run() 函数来完成有用的工作。用户并不直接调用此函数;而是必须调用 Thread 的 start() 函数,该函数再调用 run()。下面的代码说明了它的用法:
//TimePrinter.java
//Written by FoxMX
import java.util.*;class TimePrinter extends Thread {
int pauseTime;
String name;
public TimePrinter(int x, String n) {
pauseTime = x;
name = n;
}
public void run() {
while(true) {
try {
System.out.println(name + ":" + new
Date(System.currentTimeMillis()));
Thread.sleep(pauseTime);
} catch(Exception e) {
System.out.println(e);
}
}
}
static public void main(String args[]) {
TimePrinter tp1 = new TimePrinter(1000, "线程1");
tp1.start();
TimePrinter tp2 = new TimePrinter(3000, "线程2");
tp2.start();
}
}
要记住的一件重要的事情是 main() 函数也是一个线程,并可用来做有用的工作。程序员只有在需要多个线程时才需要创建新的线程。 在本例中,我们可以看到一个简单的程序,它按两个不同的时间间隔(1 秒和 3 秒)在屏幕上显示当前时间。这是通过创建两个新线程来完成的,包括 main() 共三个线程。但是,因为有时要作为线程运行的类可能已经是某个类层次的一部分,所以就不能再按这种机制创建线程。虽然在同一个类中可以实现任意数量的接口,但 Java 编程语言只允许一个类有一个父类。同时,某些程序员避免从 Thread 类导出,因为它强加了类层次。对于这种情况,就要 runnable 接口。我们来看一下使用runnable的例子:import java.util.*;
class TimePrinter_Runnable implements Runnable {
int pauseTime;
String name;
public TimePrinter_Runnable(int x, String n) {
pauseTime = x;
name = n;
}
public void run() {
while(true) {
try {
System.out.println(name + ":" + new
Date(System.currentTimeMillis()));
Thread.sleep(pauseTime);
} catch(Exception e) {
System.out.println(e);
}
}
}
static public void main(String args[]) {
Thread t1 = new Thread(new TimePrinter_Runnable(1000, "线程1"));
t1.start();
Thread t2 = new Thread(new TimePrinter_Runnable(3000, "线程2"));
t2.start();
}
}请注意,当使用 runnable 接口时,必须先建立一个Runnable对象。如上面某段程序实际上也可以这样写:
Runnable a=new TimePrinter_Runnable(1000, "线程1");
Thread t1 = new Thread(a);
t1.start();
Runnable b=new TimePrinter_Runnable(3000, "线程2");
Thread t2 = new Thread(b);
许多程序员更喜欢 runnable 接口,因为从 Thread 类继承会强加类层次。
Author : FoxMX
在现实生活中,我们经常使用多线程模型。我们在处理某些任务的同时也可以让孩子、配偶和父母完成别的任务。例如,我在写信的同时可能打发我的儿子去邮局买邮票。用软件术语来说,这称为多个控制(或执行)线程。线程是程序中的一个控制流程。
有两种生成线程的方法:
1.生成java.lang.Thread子类的新类并覆盖其run()方法。
2.声明一个实现java.lang.Runnable接口的类并将其传入Thread中运行。
Thread 类是一个具体的类,即不是抽象类,该类封装了线程的行为。要创建一个线程,程序员必须创建一个从 Thread 类导出的新类。程序员必须覆盖 Thread 的 run() 函数来完成有用的工作。用户并不直接调用此函数;而是必须调用 Thread 的 start() 函数,该函数再调用 run()。下面的代码说明了它的用法:
//TimePrinter.java
//Written by FoxMX
import java.util.*;class TimePrinter extends Thread {
int pauseTime;
String name;
public TimePrinter(int x, String n) {
pauseTime = x;
name = n;
}
public void run() {
while(true) {
try {
System.out.println(name + ":" + new
Date(System.currentTimeMillis()));
Thread.sleep(pauseTime);
} catch(Exception e) {
System.out.println(e);
}
}
}
static public void main(String args[]) {
TimePrinter tp1 = new TimePrinter(1000, "线程1");
tp1.start();
TimePrinter tp2 = new TimePrinter(3000, "线程2");
tp2.start();
}
}
要记住的一件重要的事情是 main() 函数也是一个线程,并可用来做有用的工作。程序员只有在需要多个线程时才需要创建新的线程。 在本例中,我们可以看到一个简单的程序,它按两个不同的时间间隔(1 秒和 3 秒)在屏幕上显示当前时间。这是通过创建两个新线程来完成的,包括 main() 共三个线程。但是,因为有时要作为线程运行的类可能已经是某个类层次的一部分,所以就不能再按这种机制创建线程。虽然在同一个类中可以实现任意数量的接口,但 Java 编程语言只允许一个类有一个父类。同时,某些程序员避免从 Thread 类导出,因为它强加了类层次。对于这种情况,就要 runnable 接口。我们来看一下使用runnable的例子:import java.util.*;
class TimePrinter_Runnable implements Runnable {
int pauseTime;
String name;
public TimePrinter_Runnable(int x, String n) {
pauseTime = x;
name = n;
}
public void run() {
while(true) {
try {
System.out.println(name + ":" + new
Date(System.currentTimeMillis()));
Thread.sleep(pauseTime);
} catch(Exception e) {
System.out.println(e);
}
}
}
static public void main(String args[]) {
Thread t1 = new Thread(new TimePrinter_Runnable(1000, "线程1"));
t1.start();
Thread t2 = new Thread(new TimePrinter_Runnable(3000, "线程2"));
t2.start();
}
}请注意,当使用 runnable 接口时,必须先建立一个Runnable对象。如上面某段程序实际上也可以这样写:
Runnable a=new TimePrinter_Runnable(1000, "线程1");
Thread t1 = new Thread(a);
t1.start();
Runnable b=new TimePrinter_Runnable(3000, "线程2");
Thread t2 = new Thread(b);
许多程序员更喜欢 runnable 接口,因为从 Thread 类继承会强加类层次。
有必要那样吗
我今天很高兴为了不扫兴
我给你
www.java.com.cn去下载吧
think int java是它
java.sun.com
上去看看
URL: http://jjhou.csdn.net/thinking-in-java-20010919.pdf书名:Think in Java(中文版)---chm格式
URL: http://www.code-labs.com/manual/Think%20In%20Java.chm书名:Jbuilder开发人员指南(完整版)
URL: http://www.java-cn.net/book/books-zip/JbuilderGuide.zip书名:Java2编程详解(Special_Edition_Using_Java)
URL: http://www.java-cn.net/book/books-zip/Java2Xiangjie.zip书名:Java2 轻松进阶
URL: http://www.java-cn.net/book/books-zip/java11.zip
/developerWorks/cn/education/java/j-jdbc/tutorial/j-jdbc.zip
呵呵不能去书店,就上网下吧。
上面也是别的网友提供的。
我们需要创建线程类的另一个实例。当我们构造了线程类的一个新的实例,我们必须告诉它在新的线程里应执行哪一段程序。你可以在任意实现了启动接口的对象上启动一个线程。启动接口是一个抽象接口,来表示本对象有一些函数想异步执行。要实现启动接口,一个类只需要有一个叫run的函数。下面是创建一个新线程的例子: Filename:twothread.java
class twothread implements Runnable
{
twothread()
{
Thread t1 =Thread.currentThread();
t1.setName("The first main thread");
System.out.println("The running thread:" + t1);
Thread t2 = new Thread(this,"the second thread");
System.out.println("creat another thread");
t2.start();
try
{
System.out.println("first thread will sleep");
Thread.sleep(3000);
}
catch (InterruptedException e)
{
System.out.println("first thread has wrong");
}
System.out.println("first thread exit");
}
public void run()
{
try
{
for (int i=0;i<5;i++)
{
System.out.println("Sleep time for thread 2:"+i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("thread has wrong");
}
System.out.println("second thread exit");
}
public static void main(String args[])
{
new twothread();
}
}
执行结果:java twothread
The running thread:Thread[The first main thread,5,main] creat another thread first thread will sleep Sleep time for thread 2:0 Sleep time for thread 2:1 Sleep time for thread 2:2 first thread exit Sleep time for thread 2:3 Sleep time for thread 2:4 second thread exit main线程用new Thread(this, "the second thread")创建了一个Thread对象,通过传递第一个参数来标明新线程来调用this对象的run函数。然后我们调用start函数,它将使线程从run函数开始执行。
谢谢 [email protected]