您的位置:首页 > 职场人生

【Java】【多线程常见面试题】

2017-04-02 17:05 232 查看
1.子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码

package Test;

public class Test {
static class Business{
private boolean Substop=false;

public synchronized void sub(int i){
while(Substop){
try {
this.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
for (int j = 0; j <10 ; j++) {
System.out.println("sub run"+j+"in round"+i);
}
Substop=true;
this.notifyAll();
}
public synchronized void main(int i){
while (!Substop){
try {
this.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
for (int j = 0; j <100 ; j++) {
System.out.println("main run"+j+"in round"+i);
}
Substop=false;
this.notifyAll();
}
}

public static void main(String[]  args){
final Business business=new Business();
for (int i = 0; i <50 ; i++) {
final int fi=i;
new Thread(new Runnable() {
@Override
public void run() {
// business.sub(i);     报错  只能访问方法中定义的final类型的局部变量
business.sub(fi);  //当一个对象获取对象锁的时候 其另外的同步方法都会暂停执行
}
}).start();
business.main(fi);
}
}
}


package Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
static class Business{
Lock lock=new ReentrantLock();
Condition condition=lock.newCondition();
private boolean Substop=false;
public  void sub(int i){
lock.lock();
try{
while(Substop){
try {
condition.await();
}catch (InterruptedException e){
e.printStackTrace();
}
}
for (int j = 0; j <10 ; j++) {
System.out.println("sub run"+j+"in round"+i);
}
Substop=true;
condition.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}

}
public  void main(int i){
lock.lock();
try {
while (!Substop){
try {
condition.await();
}catch (InterruptedException e){
e.printStackTrace();
}
}
for (int j = 0; j <100 ; j++) {
System.out.println("main run"+j+"in round"+i);
}
Substop=false;
condition.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}

}
}

public static void main(String[]  args){
final Business business=new Business();
for (int i = 0; i <50 ; i++) {
final int fi=i;
new Thread(new Runnable() {
@Override
public void run() {
// business.sub(i);     报错  只能访问方法中定义的final类型的局部变量
business.sub(fi);  //当一个对象获取对象锁的时候 其另外的同步方法都会暂停执行
}
}).start();
business.main(fi);
}
}
}

package Test;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
static class Business{
LinkedBlockingQueue<Integer> queue1=new LinkedBlockingQueue<>(1);
LinkedBlockingQueue<Integer>  queue2=new LinkedBlockingQueue<>(1);

{
try{
queue2.put(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public  void sub(int i){
try{
queue1.put(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for (int j = 0; j <10 ; j++) {
System.out.println("sub run"+j+"in round"+i);
}
try {
queue2.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public  void main(int i){
try{
queue2.put(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int j = 0; j <99 ; j++) {
System.out.println("main run " + j + " in round " + i);
}
try{
queue1.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void main(String[]  args){
final Business business=new Business();
for (int i = 0; i <50 ; i++) {
final int fi=i;
new Thread(new Runnable() {
@Override
public void run() {
// business.sub(i);     报错  只能访问方法中定义的final类型的局部变量
business.sub(fi);  //当一个对象获取对象锁的时候 其另外的同步方法都会暂停执行
}
}).start();
business.main(fi);
}
}
}


2.编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC

package Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test{
static class Innerclass{
Lock lock=new ReentrantLock();
Condition conditionA=lock.newCondition();
Condition conditionB=lock.newCondition();
Condition conditionC=lock.newCondition();
private String name="A";

public void printA(){
lock.lock();
try {
if (!name.equals("A")){
conditionA.await();
}
System.out.print(Thread.currentThread().getName());
Thread.sleep(1000);
name="B";
conditionB.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}

}
public void printB(){
lock.lock();
try {
if (!name.equals("B")){
conditionB.await();
}
System.out.print(Thread.currentThread().getName());
Thread.sleep(1000);
name="C";
conditionC.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}

}
public void printC(){
lock.lock();
try {
if (!name.equals("C")){
conditionC.await();
}
System.out.print(Thread.currentThread().getName());
Thread.sleep(1000);
name="A";
conditionA.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}

}
}

public static void init(){
final Innerclass innerclass=new Innerclass();
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
innerclass.printA();
}
}
});
thread.setName("A");

Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
innerclass.printB();
}
}
});
thread2.setName("B");

Thread thread3=new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
innerclass.printC();
}
}
});
thread3.setName("C");

thread.start();
thread2.start();
thread3.start();
}

public static void main(String[] args) {
Test test=new Test();
test.init();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: