在Java中实现线程的两种方法

     在Java中实现线程的两种方法

      线程总是由操作系统占有和管理,一个新线程的创建和启动只能由操作系统来负责管理。故不能直接调用该线程的run方法,若如此,则不会创建新的线程和启动该线程,而是和其它类对象一样,那么应该怎样正确启动线程呢?调用线程对象的start方法。当调用Thread对象的start方法时,就会调用一个本地的代码,该代码负责是操作系统初始化一个新的线程,并由这个线程类执行Thread对象的run方法。

     创建一个线程有两种方式:其一是继承Thread类并重载run方法;其二是实现Runnable接口。

     第一种方法的代码如下:

import java.util.*;
import java.io.*;
class ThreadTest {
	/** * Method main * * * @param args * */
	public static void main(String[] args) {
		// Create three threads CustomThread 
		first=new CustomThread("Hopalong " ,"Cassidy " ,200l);
		CustomThread second=new CustomThread("Marilyn " ,"Monroe " ,300l);
		CustomThread third=new CustomThread("Slim " ,"Pickens " ,500l);
		System.out .println("Press Enter when you hava had enought..." );
		first.start();
		second.start();
		third.start();
		try {
			System.in .read();
			// Wait until Enter key pressed 
			System.out .println("Enter pressed..." );
		}
		catch (IOException e){
			// Handle IO Exception System.out .println(e );// Output the exception } 
			System.out .println("Ending main()..." );
			return ;
		}
		// Inner 
		class private static class CustomThread extends Thread{
			// Constructor 
			public CustomThread(String firstName,String secondName,long aWhile){
				this .firstName=firstName;
				this .secondName=secondName;
				this .aWhile=aWhile;
				this .setDaemon(true );
			}
			// override run method 
			public void run(){
				try {
					while (true ){
						System.out .println(this .firstName);
						this .sleep(aWhile);
						System.out .println(this .secondName);
					}
				}
				catch (InterruptedException e){
					System.out .println(firstName+secondName+e);
				}
			}
			// Constants and Variables
			private String firstName;
			private String secondName;
			private long aWhile;
		}
	}

      第二种创建线程的方法代码如下:
 

import java.util.*;
import java.io.*;
class ThreadTest1 {
	/** * Method main * * * @param args * */
	public static void main(String[] args) {
		// Create three threads Thread
		first=new Thread(new CustomThread("Hopalong " ,"Cassidy " ,200l));
		Thread second=new Thread(new CustomThread("Marilyn " ,"Monroe " ,300l));
		Thread third=new Thread(new CustomThread("Slim " ,"Pickens " ,500l));
		System.out .println("Press Enter when you hava had enought..." );
		first.setDaemon(true );
		second.setDaemon(true );
		third.setDaemon(true );
		first.start();
		second.start();
		third.start();
		try {
			System.in .read();
			// Wait until Enter key pressed 
			System.out .println("Enter pressed..." );
		}
		catch (IOException e){
			// Handle IO Exception 
			System.out .println(e );
			// Output the exception
		}
		System.out .println("Ending main()..." );
		return ;
	}
	// Inner 
	class private static class CustomThread implements Runnable{
		// Constructor 
		public CustomThread(String firstName,String secondName,long aWhile){
			this .firstName=firstName;
			this .secondName=secondName;
			this .aWhile=aWhile;
		}
		// override run method
		public void run(){
			try {
				while (true ){
					System.out .println(this .firstName);
					Thread.sleep(aWhile);
					System.out .println(this .secondName);
				}
			}
			catch (InterruptedException e){
				System.out .println(firstName+secondName+e);
			}
		}
		// Constants and Variables 
		private String firstName;
		private String secondName;
		private long aWhile;
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值