下列程序创建了一个线程并运行,请在下划线处填入正确代码。
public class Try extends Thread{
public static void main(String args[]){
Threadt=new Try();
【 】;
}
public void run(){
System.out.println(“Try!”);
}
}
第1题:
下面程序的功能是创建一个显示5个“Hello!”的线程并启动运行。请将程序补充完整。
public class ThreadTest extends Thread {
public static void main(String args[]) {
ThreadTest t=new ______;
t.start();
}
public void run() {
int i=0;
while(true) {
System.out.println("Hello!");
if(i++==4)break;
}
}
}
第2题:
请在下划线处填入代码,是程序正常运行并且输出 “ Hello! ”
Class Test 【 15 】 {
Public static void main (String[] arge){
Test t = new Test();
t.start();
}
Public void run(){
System.out.println( “ Hello! ” );
}
第3题:
( 28 )阅读下面程序
1 public class Try extends Thread{
2 public static void main(String args[ ]){
3 Try t = new Try( );
4 t.start( );
5 }
6
7 public void run( int j){
8 int i = 0;
9 while(i<5){
10 System.out.println(" 祝你成功! ");
11 i++;
12 }
13 }
14 }
该程序要求打印 5 行 “ 祝你成功! ” ,必须改正程序中的某行代码,程序才能完成。选择正确的修改是
A )将第 1 行的 extends Thread 改为 implements Runnable
B )将第 3 行的 new Try() 改为 new Thread()
C )将第 4 行 t.start() 改为 start(t)
D )将第 7 行的 public void run( int j) 改为 public void run()
第4题:
阅读下面程序 public class Test2______ { public static void main(String[] args){ Thread t=new Test2(); t. start(); } public void run(){ System. out. priatln("How are you. "); } } 在程序下画线处填入的正确的选项是
A.implements Thread
B.extends Runnable
C.implements Runnable
D.extends Thread
第5题:
通过实现Runnable接口创建线程,请在下面横线处填入代码完成此程序。
注意:不改动程序结构,不得增行或删行。
class ThreadTest implements Runnable
{
Thread thrObj;
public static void main(String args[])
{
System.out.println("这是一个通过实现接口创建线程的例子");
ThreadTest testObj=new ThreadTest();
testObj.create();
}
public void create()
{
if(thrObj= =null)
{
thrObj=new Thread(this,"myThread");
______
}
}
public void run()
{
System.out.println("Thread"+throbj.getName()+":"+"在运行!");
}
}
第6题:
下列程序构造了一个 Swing Applet ,请在下划线处填入正确的代码
Import javax.swing.*;
Import java.awt.*;
Public class SwingApplet extends 【 13 】 {
Jlabel l = new Jlabel ( “ This is a Swing Applet. ” );
Public void init(){
Container contentPane = getContentPane();
contentPane.add(1);
}
}
第7题:
创建线程对象,要传递代码与数据,而传递代码与数据有两种方法,一是通过继承Thread类,二是向Thread类传递一个Runnable对象。请在下面程序的每条横线处填写一个语句,使程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
public class TestThread{
public static void main(String args[ ])
{
MyThread t=new MyThread();
_______________________
}
}
class MyThread_____________Thread{
_____________________
{
for(int i=0;i<10;i++){
System.out.println(" " +i);
}
}
}
第8题:
通过实现Rmmable接口创建线程,请在下面横线处填写代码完成此程序。
public class ThreadTest
{
public static void main(String args [])
{
Thread testObj1 = new Thread (new Hello ());
Thread testObj2 = new Thread (new Hello ());
testObj 2.start ( );
}
}
class Hello implements Runnable
{
int j;
public void run()
{
System.out.println("Hello" + j ++);
}
}
第9题:
阅读下面程序 1 public class Try extends Thread{ 2 public static void main(String args[]){ 3 Try t=new Try(); 4 t.start(); 5 } 6 7 public void run(int j){ 8 int i=0; 9 while(i<5){ 10 System.out.println(“祝你成功!”); 11 i++; 12 } 13 } 14 } 该程序要求打印5行“祝你成功!”,必须改正程序中的某行代码,程序才能完成。选择正确的修改是
A.将第1行的extends Thread改为implements Runnable
B.将第3行的new Try()改为new Thread()
C.将第4行t.start()改为start(t)
D.将第7行的public void run(int j)改为public void run()
第10题:
阅读下面程序 1 public class Try extends Thread { 2 public static void main(String args[]) { 3 Try t=new Try(); 4 t.start(); 5 } 6 7 public void run(int j) { 8 int i=0; 9 while(i<5) { 10 System.out.println("祝你成功!"); 11 i++: 12 } 13 } 14 } 该程序若能打印5行“祝你成功!”,必须改正程序中的某行代码,选择正确的修改是
A.将第1行的extends Thread改为implements Runnable
B.将第3行的new Try()改为new Thread()
C.将第4行的t.start()改为start(t)
D.将第7行的public void run(int j)改为public void run()