好文档 - 专业文书写作范文服务资料分享网站

Java面试宝典2012版 

天下 分享 时间: 加入收藏 我要投稿 点赞

private Lock lock = new ReentrantLock(); public static void main(String[] args) { }

private class Subtractor implements Runnable { }

private class Adder implements Runnable {

@Override

public void run() {

// TODO Auto-generated method stub while(true) { @Override

public void run() { }

// TODO Auto-generated method stub while(true) { }

/*synchronized (ThreadTest.this) {

System.out.println(\ //这里抛异常了,锁能释放吗? }*/

lock.lock(); try { { }

lock.unlock();

System.out.println(\}finally

// TODO Auto-generated method stub ThreadTest tt = new ThreadTest(); for(int i=0;i<2;i++) { }

new Thread(tt.new Adder()).start(); new Thread(tt.new Subtractor()).start();

/*synchronized (ThreadTest.this) { System.out.println(\ }*/

lock.lock(); try { System.out.println(\ }finally

{

lock.unlock();

}

}

}

}

}

55、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。

以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题。 public class ThreadTest1 {

private int j;

public static void main(String args[]){ ThreadTest1 tt=new ThreadTest1(); Inc inc=tt.new Inc(); Dec dec=tt.new Dec(); for(int i=0;i<2;i++){

Thread t=new Thread(inc); t.start();

t=new Thread(dec); t.start(); } }

private synchronized void inc(){ j++;

System.out.println(Thread.currentThread().getName()+\ }

private synchronized void dec(){ j--;

System.out.println(Thread.currentThread().getName()+\ }

class Inc implements Runnable{ public void run(){

for(int i=0;i<100;i++){ inc(); } } }

class Dec implements Runnable{ public void run(){

for(int i=0;i<100;i++){ dec(); } } } }

----------随手再写的一个------------- class A {

JManger j =new JManager(); main() {

new A().call(); }

void call {

for(int i=0;i<2;i++) {

new Thread( new Runnable(){ public void run(){while(true){j.accumulate()}}} ).start();

new Thread(new Runnable(){ public void run(){while(true){j.sub()}}}).start(); } } }

class JManager {

private j = 0;

public synchronized void subtract() { j--

}

public synchronized void accumulate() {

j++; } }

56、子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序。

最终的程序代码如下:

public class ThreadTest {

}

public void run() { }

for(int i=0;i<50;i++) {

business.SubThread(i);

}

public void init() {

final Business business = new Business(); new Thread(

new Runnable() {

} /**

* @param args */

public static void main(String[] args) {

// TODO Auto-generated method stub new ThreadTest().init();

}

).start();

for(int i=0;i<50;i++) {

business.MainThread(i);

}

private class Business {

boolean bShouldSub = true;//这里相当于定义了控制该谁执行的一个信号灯 public synchronized void MainThread(int i) { }

public synchronized void SubThread(int i) {

if(!bShouldSub)

try {

this.wait();

// TODO Auto-generated catch block e.printStackTrace();

} catch (InterruptedException e) { if(bShouldSub) { }

bShouldSub = true; this.notify();

System.out.println(Thread.currentThread().getName() + try {

this.wait();

// TODO Auto-generated catch block e.printStackTrace();

} catch (InterruptedException e) {

}

for(int j=0;j<5;j++)

\

}

for(int j=0;j<10;j++)

Java面试宝典2012版 

privateLocklock=newReentrantLock();publicstaticvoidmain(String[]args){}privateclassSubtractorimplementsRunn
推荐度:
点击下载文档文档为doc格式
8ldap6fbzl4zk8m0i2f7
领取福利

微信扫码领取福利

微信扫码分享