更多"创建线程对象,要传递代码与数据,而传递代码与数据有两种方法,一是通过继"的相关试题:
[简答题]创建线程对象,要传递代码与数据,而传递代码与数据有两种方法,一是通过继承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);
[填空题]提供线程体的特定对象是在创建线程时指定的;创建线程对象是通过调用 【6】 类的构造方法实现的。
[填空题]创建一个线程之后,通过调用该线程对象的______方法将潜入该线程的虚拟CPU置于可运行状态。
[填空题]通过实现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 ++);
}
}
[填空题]通过实现Runnable接口创建线程,请在画线处加入正确的代码完成此程序 【9】 。
public class ThreadTest
public static void main(String args[ ]
Thread t1 = new Thread(new Hello( )
Thread t2 = new Thread(new Hello( )
___________;
t2.start( );
class Hello implements Runnable
int i;
public void run( )
while (true)
System.out.println("Hello"+i++
if(i==5) break;
[简答题]通过实现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( )+":"+"在运行!");
}
}
[填空题]请在下面横线处填入适当的代码,将线程对象runner[2]的优先级设置为最高。
class ThreadTest extends Thread
public static void main(String args[])
{
ThreadTest[] runner = new ThreadTest[4];
for(int j = 0; j<4; j++) runner[j] = new ThreadTest( );
for(int j = 0; j<4; j++) runner[j].start( );
【7】
runner[3].setPriority(MIN_PRIORITY);
}
public void run{)
{
for(int i = 0; i<100000; i++);
System.out.println(getName( ) + "线程的优先级是:"
+ getPriority( ) + "计算完毕");
[填空题]通过实现Runnable接口创建线程,请在画线处加入正确的代码完成此程序______。
publicclass ThreadTest
publicstaticvoid main(Stringargs[])
Threadt1=newThread(newHello( )):
Threadt2=newThread(newHellO( ));
T2.start( );
classHelloimplementsRunnable
intI;
publicvoidIUB( )
while(true)
System.out.println(“Hello”+i++);
If(i==5)break;
[填空题]通过实现Runnable接口创建线程,请在画线处加入正确的代码完成此程序______。
publicclass ThreadTest
{
publicstaticvoid main(Stringargs[])
{
Threadt1=newThread(newHello( )):
Threadt2=newThread(newHellO( ));
T2.start( );
}
}
classHelloimplementsRunnable
{
intI;
publicvoidIUB( )
{
while(true)
{
System.out.println(“Hello”+i++);
If(i==5)break;
}
}
}
[简答题]通过实现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( )+":"+"在运行!");
[填空题]通过实现Runnable接口创建线程,请在画线处加入正确的代码完成此程序_________。
public class ThreadTest
public static void main(String args[])
Thread t1=new Thread(new Hello( )):
Thread t2=new Thread(new Hello( ));
________;
t2.start( );
class Hello implements Runnable
int i;
public void run( )
while(true)
System.out.println("Hello"+i++);
If(i==5) break:
[单项选择]在创建线程时可以显式地指定线程组,此时可供选择的线程构造方法有______种。
A. 1
B. 2
C. 3
D. 4
[填空题]通过继承Thread创建线程,在主控程序中同时运行两个线程Thread1和Thread2。请在下面横线处填入代码完成此程序。
public class ThreadTest
public static void main(String args[])
new TestThread("Threadl").start( );
【7】
class TestThread extends Thread( )
public TestThread(String str)
super (str);
public void run( )
for(int i = 0; i<’5; i++)
System.out.println(i + .... + getName( ) + "在运行");
try
Sleep(1000);
catch(InerruptedException e)
System.out.println(getName( ) + "已结束");
[填空题]一个程序中单独的、并发的线程对同一个对象进行访问的代码段,称为【 】。