java分享第八天-01(线程)

 创建线程:
1 可以实现Runnable接口。
2 可以扩展Thread类本身。
通过实现Runnable创建线程:

创建一个线程,最简单的方法是创建一个实现Runnable接口的类。

为了实现Runnable,这个类需要实现只有一个单一的方法 run(),它是这样声明的:

public void run( )
定义构成新线程 run()方法的代码内部。重要的是要明白的run()可以调用其他方法,使用其他类,并声明变量,就像主线程可以是很重要的。 
当创建一个实现Runnable类,会从类中实例化线程的对象。线程定义了多个构造函数。我们将使用一个如下所示:
Thread(Runnable threadOb, String threadName);
在这里,threadOb是实现Runnable接口和新线程的名称是由threadName指定一个类的实例。
创建新线程后,它不会启动运行,直到调用它的start()方法,它是内线程声明。start()方法如下所示:
void start( ); 

 下面是创建一个新的线程并开始运行一个例子:
// Create a new thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
} // This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
} public class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(100);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}

这将产生以下结果:

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting. 

 
 通过扩展Thread创建线程:
创建一个线程的第二种方式是创建可扩展条条一个新的类,然后创建一个类的实例。
扩展类必须重写run()方法,这是切入点的新线程。它还必须调用start()开始执行新线程。
例子:
下面是重写扩展线程前面的程序:
// Create a second thread by extending Thread
class NewThread extends Thread {
   NewThread() {
      // Create a new, second thread
      super("Demo Thread");
      System.out.println("Child thread: " + this);
      start(); // Start the thread
   }

// This is the entry point for the second thread.
   public void run() {
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Child Thread: " + i);
// Let the thread sleep for a while.
            Thread.sleep(50);
         }
      } catch (InterruptedException e) {
         System.out.println("Child interrupted.");
      }
      System.out.println("Exiting child thread.");
   }
}

public class ExtendThread {
   public static void main(String args[]) {
      new NewThread(); // create a new thread
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Main Thread: " + i);
            Thread.sleep(100);
         }
      } catch (InterruptedException e) {
         System.out.println("Main thread interrupted.");
      }
      System.out.println("Main thread exiting.");
   }
}
这将产生以下结果:

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting. 

上一篇:PPPOE拨号演练


下一篇:python下操作ftp上传