java基础之线程练习题

发布时间:2024年01月16日

1.关于线程设计,下列描述正确的是()

A.线程对象必须实现Runnable?接口

B.启动一个线程直接调用线程对象的 run()方法

C.Java?对多线程同步提供语言级的支持????synchronized

D.一个线程可以包括多个进程

2.在java?中的线程模型包含()

? ? ? ? ? ? ?A.?一个虚拟处理器(CPU) B.?的代码

? ? ? ? ? ? C.?操作数据 D.?以上都不是

3.有关线程,那些描述是正确的?

A.一旦一个线程被创建,它就立即开始运行??sleep

B.使用start()方法可以是一个线程成为可运行的,但它不一定立即开始运行

C.最多只能有一个线程处于就绪状态

?

D.调用sleep?方法可以让当前线程终止

4.Runnable?接口中定义了如下的哪些方法()

A.start() B.?run() C.?stop() D. sleep()

5.Thread?类位于以下哪一个包中()

? ????????????????A.?java.lang B.?java.util C.?java.math D.?java.util.concurrent

6.以下哪个方法是用于定于一个线程的执行体()

??????????????????A.?start() B.?main() C.?run() D.?init()

7.线程调用sleep?方法后,线程会进入的状态是()

? ? ? ? ? ? ? ? ? A.?就绪状态 B.?可运行状态 C.?期等状态 D.?终止状态

8.以下哪些情况可以终止当前线程的运行()

A.抛出一个异常时

B.当该线程调用sleep?方法时

C.当创建一个新的线程时

D.当线程调用join?方法时

9.关于线程,判断以下描述的是否正确

  1. Java?线程有多种不同的状态,这几种状态中的任何两种状态之间都可以相互转换。(F)?消亡
  2. 线程的启动可以执行调用 run?方法。(F)??启动start ?run可以运行 ?
  3. 当线程所定义的 run?方法执行完毕时,线程就会终止。(T)?消亡
?
  1. 使用Thread?子类创建线程的优点是可以在子类中增加新的成员使线程具有某种属性,也可以在子类中新增加方法,使线程具有某种功能;但是 Java?不支持多继承,Thread?类的子类不能再扩展其他的类。(T)

10.仔细阅读以下代码,关于程序描述正确的是()

A.代码编译失败,因为ex2.run()无法获得执行

B.代码编译成功,存在 3?个可运行的线程

C.代码编译成功,存在 1?个可运行的线程

D.以上描述都不正确

11.仔细阅读以下代码,关于程序描述正确的是()

A.打印输出,从 0?至limit

B.无内容输出,因为没有明确调用run()方法

C.代码编译失败,因为没有正确实现 Runnable?接口

D.代码编译失败,如果声明类为抽象类,可使代码编译成功

E.代码编译失败,如果去掉implements?Runnable,可使代码编译成功。

?

12.运行结果是()

A.?出错???抛出异常 B.?运行时异常

C.?正常编译运行,输出?sleep D.?正常编译运行,但没有内容输出

13.仔细阅读以下代码,将程序中不正确的地方进行改正。

package com.by.homework8Thread;


class Mythread1 {
????public void run(){

????}
}
class MyThread2 extends Thread{
????public void run() {
????????for (int i = 0; i < 100; i++) {
????????????try {
????????????????Thread.sleep((int)Math.random()*10000);
????????????} catch (InterruptedException e) {
????????????????throw new RuntimeException(e);
????????????}
????????????System.out.println("word");
????????}
????}
}


public class TestMyThread {
????public static void main(String[] args) {
????????Thread t1=new Thread(new Runnable() {
????????????@Override
????????????public void run() {
????????????????for (int i = 0; i < 100; i++) {
????????????????????try {
????????????????????????Thread.sleep((int)Math.random()*10000);
????????????????????} catch (InterruptedException e) {
????????????????????????throw new RuntimeException(e);
????????????????????}
????????????????????System.out.println("hello");
????????????????}
????????????}
????????});
????????Thread t2=new MyThread2();
????????t1.start();
????????t2.start();
????}
}

14.编程:创建两个线程,要求如下:(1)一个线程输出1~26,另一个线程输出A~Z(2)一个线程使用继承Thread类的写法,另一个线程使用实现Runnable接口的写法

package com.by.homework8Thread;

public class MyThread extends Thread{
????@Override
????public void run() {
????????for (int i = 1; i <=26 ; i++) {
????????????System.out.println("继承Thread:"+i);
????????}
????}
}



package com.by.homework8Thread;

public class MyRunnable implements Runnable{
????@Override
????public void run() {
????????char c=0;
????????for (int i = 65; i <=90 ; i++) {
????????????c= (char) i;
????????????System.out.println("实现Runnable:"+c);
????????}
????}
}



package com.by.homework8Thread;

public class Test14 {
????public static void main(String[] args) {
????????Thread t1=new MyThread();
????????Thread t2=new Thread(new MyRunnable());
????????t1.start();
????????t2.start();
????}
}

15.编程:假设一个银行的ATM机,它允许用户存款也可以取款。现在一个账户(Account)上存款为200元,用户A和用户B都拥有这个账户存款和取款的权利。用户A存入100元,而用户B将取出50元,则最终账户的存款应为250元。为了模拟以上过程,描述如下:(1)定义一个Account类,属性为账户名(userName)和余额(balance),类进行封装(2)线程ThreadA负责存钱操作;线程ThreadB负责取钱操作;(3)main函数中负责开启两个线程,并将存取操作结束之后的账户余额进行打印输出

package com.by.homework8Thread;

public class Account {
????private String userName;
????private double balance;

????public Account() {
????}

????public Account(String userName, double balance) {
????????this.userName = userName;
????????this.balance = balance;
????}

????public String getUserName() {
????????return userName;
????}

????public void setUserName(String userName) {
????????this.userName = userName;
????}

????public double getBalance() {
????????return balance;
????}

????public void setBalance(double balance) {
????????this.balance = balance;
????}

????@Override
????public String toString() {
????????return "Account{" +
????????????????"userName='" + userName + '\'' +
????????????????", balance=" + balance +
????????????????'}';
????}
}



package com.by.homework8Thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AccountTest {
????public static void main(String[] args) {
????????Account a=new Account("银行余额",200);
????????Thread t1=new Thread(()->{
????????????a.setBalance(a.getBalance()+100);
????????});
????????Thread t2=new Thread(()->{
????????????a.setBalance(a.getBalance()-50);
????????});
????????ExecutorService es= Executors.newFixedThreadPool(2);
????????es.submit(t1);
????????es.submit(t2);
????????es.shutdown();
????????System.out.println("余额为:"+a.getBalance());
????}
}

16.编程:(1)一个线程任务打印输出100个”&&&&&&”;一个线程任务打印输出100个”***********”(2)完成以上两个线程任务之后,继续完成一个线程任务打印输出100个”~~~~~~~~~~”;一个线层任务打印输出100个”#############”

package com.by.homework8Thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test16 {
????public static void main(String[] args) throws InterruptedException {
????????Thread t1=new Thread(new Runnable() {
????????????@Override
????????????public void run() {
????????????????for (int i = 0; i < 100; i++) {
????????????????????System.out.print("&");
????????????????}
????????????}
????????});
????????Thread t2=new Thread(()->{
????????????try {
????????????????t1.join();
????????????} catch (InterruptedException e) {
????????????????throw new RuntimeException(e);
????????????}
????????????for (int i = 0; i < 100; i++) {
????????????????System.out.print("*");
????????????}
????????});
????????Thread t3=new Thread(()->{
????????????for (int i = 0; i < 100; i++) {
????????????????try {

????????????????????t2.join();
????????????????} catch (InterruptedException e) {
????????????????????throw new RuntimeException(e);
????????????????}
????????????????System.out.print("~");
????????????}
????????});
????????Thread t4=new Thread(new Runnable() {
????????????@Override
????????????public void run() {
????????????????for (int i = 0; i < 100; i++) {
????????????????????try {
????????????????????????t2.join();
????????????????????} catch (InterruptedException e) {
????????????????????????throw new RuntimeException(e);
????????????????????}
????????????????????System.out.print("#");
????????????????}
????????????}
????????});
????????ExecutorService es= Executors.newFixedThreadPool(2);
????????es.submit(t1);
????????t1.join();
????????System.out.println();
????????es.submit(t2);
????????t2.join();
????????System.out.println();
????????t3.join();
????????es.submit(t3);
????????System.out.println();
????????es.submit(t4);

????????es.shutdown();
????}
}

文章来源:https://blog.csdn.net/zhzjn/article/details/135628060
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。