A.线程对象必须实现Runnable?接口
B.启动一个线程直接调用线程对象的 run()方法
C.Java?对多线程同步提供语言级的支持????synchronized
D.一个线程可以包括多个进程
? ? ? ? ? ? ?A.?一个虚拟处理器(CPU) B.?执行的代码
? ? ? ? ? ? C.?代码操作的数据 D.?以上都不是
A.一旦一个线程被创建,它就立即开始运行??sleep
B.使用start()方法可以是一个线程成为可运行的,但它不一定立即开始运行
C.最多只能有一个线程处于就绪状态
D.调用sleep?方法可以让当前线程终止
A.start() B.?run() C.?stop() D. sleep()
? ????????????????A.?java.lang B.?java.util C.?java.math D.?java.util.concurrent
??????????????????A.?start() B.?main() C.?run() D.?init()
? ? ? ? ? ? ? ? ? A.?就绪状态 B.?可运行状态 C.?有限期等待状态 D.?终止状态
A.抛出一个异常时
B.当该线程调用sleep?方法时
C.当创建一个新的线程时
D.当线程调用join?方法时
A.代码编译失败,因为ex2.run()无法获得执行
B.代码编译成功,存在 3?个可运行的线程
C.代码编译成功,存在 1?个可运行的线程
D.以上描述都不正确
A.打印输出,从 0?至limit
B.无内容输出,因为没有明确调用run()方法
C.代码编译失败,因为没有正确实现 Runnable?接口
D.代码编译失败,如果声明类为抽象类,可使代码编译成功
E.代码编译失败,如果去掉implements?Runnable,可使代码编译成功。
A.?编译出错???抛出异常 B.?运行时异常
C.?正常编译运行,输出?sleep D.?正常编译运行,但没有内容输出
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();
????}
}