【Java】【多线程常见面试题】
2017-04-02 17:05
274 查看
1.子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码
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();
}
}
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();
}
}
相关文章推荐
- Java多线程常见面试题
- java多线程常见面试题
- Java常见面试题—实现多线程的三种方式
- java基础--31.多线程常见的面试题
- Java多线程面试题总结(常见多线程面试题)
- JAVA面试题解惑系列(十)——话说多线程
- JAVA面试题解惑系列(十)——话说多线程
- java常见面试题
- java常见面试题
- 常见的JAVA面试题
- 编写多线程Java应用程序常见问题
- 编写多线程Java应用程序常见问题
- JAVA面试题解惑系列(十)——话说多线程
- Java常见面试题集--面试题全面综合(一)
- Java常见面试题集--面试题全面综合(二)
- (转)Java常见面试题集--面试题全面综合(一)
- 编写多线程Java应用程序常见问题
- Java常见面试题(含答案)
- Java常见面试题集--面试题全面综合(一)
- Java常见面试题集--面试题全面综合(三)