NIIT认证

NIIT认证面试真题及答案

时间:2024-08-16 01:04:43 NIIT认证 我要投稿

NIIT认证面试真题及答案

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

NIIT认证面试真题及答案

  创建一个线程有两种方式:其一是继承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;

  }

  }

【NIIT认证面试真题及答案】相关文章:

NIIT认证面试真题及答案2017年05-11

NIIT认证Java面试题及答案08-27

NIIT认证简介08-15

2017年Java认证考试真题及答案09-14

NIIT认证的就业前景06-26

NIIT教学及认证体系09-16

NIIT认证详细介绍07-09

关于NIIT认证的介绍06-16

NIIT认证培训笔记08-18

2016关于NIIT认证的问与答06-21