NIIT认证面试真题及答案
线程总是由操作系统占有和管理,一个新线程的创建和启动只能由操作系统来负责管理。故不能直接调用该线程的run方法,若如此,则不会创建新的`线程和启动该线程,而是和其它类对象一样,那么应该怎样正确启动线程呢?调用线程对象的start方法。当调用Thread对象的start方法时,就会调用一个本地的代码,该代码负责是操作系统初始化一个新的线程,并由这个线程类执行Thread对象的run方法。
创建一个线程有两种方式:其一是继承Thread类并重载run方法;其二是实现Runnable接口。
第一种方法的代码如下:
import .*;
import .*;
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);
tln("Press Enter when you hava had enought...");
t();
t();
t();
try{
();// Wait until Enter key pressed
tln("Enter pressed...");
}
catch(IOException e){// Handle IO Exception
tln(e );// Output the exception
}
tln("Ending main()...");
return;
}
// Inner class
private static class CustomThread extends Thread{
// Constructor
public CustomThread(String firstName,String secondName,long aWhile){
tName=firstName;
ndName=secondName;
le=aWhile;
aemon(true);
}
// override run method
public void run(){
try{
while(true){
sp; tln(tName);
p(aWhile);
tln(ndName);
}
}
catch(InterruptedException e){
tln(firstName+secondName+e);
}
}
// Constants and Variables
private String firstName;
private String secondName;
private long aWhile;
}
}
第二种创建线程的方法代码如下:
import .*;
import .*;
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));
tln("Press Enter when you hava had enought...");
aemon(true);
aemon(true);
aemon(true);
t();
t();
t();
try{
();// Wait until Enter key pressed
tln("Enter pressed...");
}
catch(IOException e){// Handle IO Exception
tln(e );// Output the exception
}
tln("Ending main()...");
return;
}
// Inner class
Private static class CustomThread implements Runnable{
// Constructor
public CustomThread(String firstName,String secondName,long aWhile){
tName=firstName;
ndName=secondName;
le=aWhile;
}
// override run method
public void run(){
try{
while(true){
tln(tName);
p(aWhile);
tln(ndName);
}
}
catch(InterruptedException e){
tln(firstName+secondName+e);
}
}
// Constants and Variables
private String firstName;
private String secondName;
private long aWhile;
}
}
-
2016年Niit认证mvc1试题
1、判断以下代码的输出:namespaceOOP{classProgram{publicvoidChange(outintx){x=100;}staticvoidMain(string[]args){ProgramP=newProgram();intx=10;ge(outx);eLine("x={0}",x);}}}A.x=10B.x=100C.x=110D.编译错误2、保留与特定用途有关的信息的称作:A.抽象B....
-
初级网络工程师简介
网络工程师是指基于硬、软件两方面的工程师,根据硬件和软件的不同、认证的不同,将网络工程师划分成很多种类。下面是小编整理的初级网络工程师简介,希望大家认真阅读!网络基础知识:深度网络基本概念,例如>ISO/OSI、TCP/IP、VLAN、各种LAN、WAN协议、各种路由协议、N...
-
iOS基础问答面试题
iOS7中的Siri拥有新外观、新声音和新功能。它的界面经过重新设计,以淡入视图浮现于任意屏幕画面的最上层。以下是小编整理的iOS基础问答面试题,希望大家认真阅读!touseNSMutableArrayandwhentouseNSArray?什么时候使用NSMutableArray,什么时候使用NSArray?答案:当...
-
NIIT认证Java面试题及答案
NIIT认证Java面试题有哪些?下面跟yjbys小编一起来学习吧!希望对同学们面试有所帮助!1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括...