Java认证考试实例疑难辨析(4)

4.

【知识点】

(1)同步化方法(Synchronized Method)

同步化方法是Java实现同步化机制的两种基本形式之一,只要将synchronized关键词加入方法声明中就可以让方法成为同步化方法。

同步化方法的特点是同一对象中的同步化方法不允许同时被不同线程调用,即使是不同的同步化方法也不允许同时调用,也就是说,如果有一个线程调用了某个对象中的同步化方法,那么其他线程就不能同时调用这个对象中的所有同步化方法,因为每个对象只有一个内部锁,任何线程要调用同步化方法,必须拥有这个对象内部锁。

线程调用的同步化方法运行结束时,内部锁会自动归还方法所属的对象,其他线程就可以取得内部锁,然后调用该对象的同步化方法。

同步化方法可以确保对象状态改变对所有线程都是统一可见的,避免了多个线程调用同一对象中方法造成对象中字段值等状态信息的不一致。

【例题】

Given:

class PingPong2 {

synchronizedvoid hit(long n) {

for(int i= 1; i < 3; i++)

System.out.print(n+ "-" + i + " ");

}

}

public class Tester implements Runnable {

staticPingPong2 pp2 = new PingPong2();

publicstatic void main(String[] args) {

newThread(new Tester()).start();

newThread(new Tester()).start();

}

publicvoid run() { pp2.hit(Thread.currentThread().getId()); }

}

Which statement is true?

A. The output could be 5-1 6-1 6-2 5-2

B. The output could be 6-1 6-2 5-1 5-2

C. The output could be 6-1 5-2 6-2 5-1

D. The output could be 6-1 6-2 5-1 7-1

【Answer】 B

【例题辨析】

(1)主类Tester实现了线程方法run(),run()的主要工作就是取得当前线程号,main()方法创建了两个Tester类的线程对象,既然有两个线程对象,也就意味着run()代码在内存映像中有两份,即这两个run()方法是各自独立运行在两个不同线程中,因此,Thread.currentThread().getId()取得的是各自的线程号;

(2)线程号取得后作为参数传递给PingPong2类对象pp2的hit()方法,hit()方法负责显示输出信息,hit()是同步化方法,也就意味着当某个线程调用它时,另外的线程必须等待它运行结束,才能取得调用hit()的资格。

A. 从输出信息可以看出,5号线程调用同步化方法hit()时,该方法中循环还未运行结束,6号线程就又调用hit()输出信息,这违背了同步化方法机制;

B. 从输出信息可以看出,6号线程调用同步化方法hit()结束后,5号线程才能调用hit()输出信息,这符合了同步化方法机制;

C. 同A

D. 出现6、5、7三个线程号,显然不符合本题只有2个线程。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,根据提供的引用内容,没有提供具体的Java在线考试系统的编程实例。但是,我可以为您提供一个基本的Java在线考试系统的编程框架,供您参考: ```java import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Question { private String question; private List<String> options; private int correctOption; public Question(String question, List<String> options, int correctOption) { this.question = question; this.options = options; this.correctOption = correctOption; } public String getQuestion() { return question; } public List<String> getOptions() { return options; } public int getCorrectOption() { return correctOption; } } class Exam { private List<Question> questions; public Exam(List<Question> questions) { this.questions = questions; } public void startExam() { int score = 0; Scanner scanner = new Scanner(System.in); for (int i = 0; i < questions.size(); i++) { Question question = questions.get(i); System.out.println("Question " + (i + 1) + ": " + question.getQuestion()); List<String> options = question.getOptions(); for (int j = 0; j < options.size(); j++) { System.out.println((j + 1) + ". " + options.get(j)); } System.out.print("Enter your answer: "); int answer = scanner.nextInt(); if (answer == question.getCorrectOption()) { score++; } } System.out.println("Your score: " + score + "/" + questions.size()); } } public class OnlineExamSystem { public static void main(String[] args) { List<Question> questions = new ArrayList<>(); questions.add(new Question("What is the capital of France?", List.of("London", "Paris", "Berlin", "Madrid"), 2)); questions.add(new Question("What is the largest planet in our solar system?", List.of("Mars", "Jupiter", "Earth", "Saturn"), 2)); questions.add(new Question("What is the chemical symbol for gold?", List.of("Au", "Ag", "Fe", "Cu"), 1)); Exam exam = new Exam(questions); exam.startExam(); } } ``` 这是一个简单的Java在线考试系统的编程示例。它包括了问题类(Question)和考试类(Exam),并在主类(OnlineExamSystem)中创建了一些问题并开始考试。您可以根据自己的需求进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值