NIIT认证面试真题及答案

发布时间:2016-10-26 00:00:00 编辑:嘉辉 手机版

  线程总是由操作系统占有和管理,一个新线程的创建和启动只能由操作系统来负责管理。故不能直接调用该线程的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){

  sp; 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;

  }

  }

本文已影响868
+1
0