问题 问答题

请完成下列Java程序:运行3个线程,每一个线程有自己的标志,用a,b,c表示,每个线程显示一个“Start”信息和一个“End”信息并且间隔地显示2个“Loop”信息(间隔变化为(0.5-2)秒之间的随机延迟)。
程序运行结果如下:(注:由于时间间隔为随机数,所以,运行结果的顺序不惟一)
a Start
b Start
c Start
b Loop
a Loop
b Loop
b End
c Loop
a Loop
a End
c Loop
c End
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
public class ex2_2 implements Runnable
static char flag2_2 = ’a’;
public static void main(String[] arg)
ex2_2 obj2_2 = new ex2_2();
Thread thread2_2 = new Thread(obi2_2);
thread2_2.start();
thread2_2 = new Thread(obj2_2);
thread2 2.start();
thread2_2 = new Thread(obi2_2);
thread2_2.start ( );

public void run()
char myflag2_2;
synchronized(this)
_________________;

System.out.println(myflag2_2 +" Start");
for(int i=0; i<2; i++)
try
Thread.sleep(rand(500,2000));
System.out.println(myflag2_2 +" Loop");
catch(InterruptedException ie)
System.out.println(ie);


System.out.println(myflag2_2 +" End");

final private int rand(int low, int high)
return(_________________);

答案

参考答案:myflag2_2 = flag2_2++
((int)((high-low+1)*(Math.random())))+low

解析:
本题主要考查线程的创建与同步和控制随机数产生范围的算法。解题关键是,熟悉线程的概念和使用,线程的同步控制, char类型数据的操作,以及熟悉随机数产生的方法Math.random(),并且能够进一步根据需要控制随机数产生的范围。在本题中,main()方法中创建3个线程,这3个线程使用obj2_2对象。线程调用start()方法来中断,使一个新的线程开始执行,并且调用run()方法。第1空,run()方法为自己设置一个线程的标志,并为下一个线程的使用增加这个标志值,由于此时线程正在处理中,因此在同步块中完成修改标志的语句放在同步块中。第2个空,通过low和high两个参数,控制产生随机数的范围,将基本的数学思想应用于程序设计中。

问答题 简答题
阅读理解

“Migrant workers”(外来务工人员)is a familiar term for 17 -year- old Shi Jian. Her father and uncle often talk about the problems surrounding this group of people. However, Shi cares more about the children of these migrant workers.

Last year Shi’s housekeeper’s daughter had to give up her schooling in Beijing because she would not be allowed to sit the national college entrance exam. She was forced to continue her high school education in her hometown in Sichuan Province.

Shi thinks the current law is unfair for migrant workers’ children, as it prevents them from studying at public schools, unless their parents pay huge sums. So, Shi decided to do a survey of migrant workers so that she could find some solutions to help solve the problem.

In the last six months Shi has spoken to 186 migrant workers in Beijing. Her interviewees include babysitters, guards, keepers, supermarket clerks, house cleaners and construction site workers. Shi found out that 55 of them were parents. Among this group, only eight have children who attend schools in Beijing. The other 47 had to leave children with relatives in the countryside. However, 40 of the 47 would like to enroll(使入学)their children in city schools.

From her interviews, Shi found that the workers’ lowest income was 400 yuan each month. Over one - third earn a monthly wage of or below 1000 yuan. “Even schools for migrant workers’ children charge more than public schools. Moreover, parents constantly worry about these schools closing or relocating,” she explained.

Shi has recently completed a report about her survey, in which she makes a series of recommendations. “The government can set up public schools for migrants’ children equal to schools for city children. Second, to offset(弥补)migrant workers’ very low pay, public schools should offer them special rates so they can afford their children’s tuition(学费).

1. What drove Shi to do the survey of migrant workers?

A. She wanted to draw people’s attention and become famous.

B. Her housekeeper’s daughter was forced to move to her hometown to continue studying.

C. She was asked to do a survey of migrant workers as part of a school project.

D. Her curiosity about migrant workers’lives.

2. According to Shi’s survey, most migrant workers’kids ________.

A. have to help their parents support the family

B. attend schools near their parents’ working places

C. don’t live together with their parents

D. always do better in schools than children from the city

3. All of the following prevent migrant workers’ kids from attending schools in cities EXCEPT ________.

A. their parents’ low income

B. that schools for migrant workers’ children may close or relocate

C. schools’ high tuitions

D. their lack of confidence to face the fierce competitions in city schools

4. Shi suggested in her survey that ________.

A. children’s tuition should be reduced

B. more money should be collected for migrants’ children

C. more private schools for migrants’ children should be built

D. students from the city and the countryside should be encouraged to communicate more