黑马程序员技术交流社区
标题:
一个面试编程题,看之前有人发的,感觉好拖沓,求解
[打印本页]
作者:
汤汤微微
时间:
2015-1-4 13:04
标题:
一个面试编程题,看之前有人发的,感觉好拖沓,求解
声明一个共享数组,起两个线程,两个线程分别隔一段时间(可以写一个随机数),给数组中添加数据,每一个线程为数组添加3个数据即可(最好实现两个线程交替添加,不要用基础中没学过的内容),谢谢大家了
作者:
jant60
时间:
2015-1-4 13:36
可以用锁 lock,或者synchronized 线程里面run方法 每次循环给数组副三次值就行了
作者:
godmmm
时间:
2015-1-4 14:19
没写注释,自己看看吧,我也捣鼓了好久才弄懂的。
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*声明一个共享数组,起两个线程,两个线程分别隔一段时间(可以写一个随机数),给数组中添加数据,每一个线程为数组添加3个数据即可。*/
public class TwoThreadToArray {
public static void main(String[] args) {
final Rec2 r=new Rec2();
Thread t1=new Thread(new Runnable(){
public void run()
{
for(int x=0;x<3;x++)
{
r.addFirst(x,r.num++);
}
}
},"w1");
Thread t2=new Thread(new Runnable(){
public void run()
{
for(int x=0;x<3;x++)
{
r.addSecond(x,r.num++);
}
}
},"w2");
t1.start();
t2.start();
try {t1.join(); } catch (InterruptedException e) {e.printStackTrace();}
try {t2.join(); } catch (InterruptedException e) {e.printStackTrace();}
System.out.println("--------------");
for(int i=0;i<r.arr.length;i++)
{
System.out.println("arr["+i+"]="+r.arr[i]);
}
}
}
class Rec2
{
int[] arr=new int[6];
boolean flag;
int num;
Lock lock=new ReentrantLock();
Condition conditon_fr=lock.newCondition();
Condition conditon_se=lock.newCondition();
public void addFirst(int x,int num) {
lock.lock();
try{
while(flag)
{
conditon_fr.await();
}
Thread.sleep(100);
arr[num]=new Random().nextInt(10);
System.out.println(Thread.currentThread().getName()+"first--"+"arr["+num+"]"+arr[num]);
flag=true;
conditon_se.signal();
}
catch(Exception e)
{
}
finally
{
lock.unlock();
}
}
public void addSecond(int x,int num) {
lock.lock();
try{
while(!flag)
{
conditon_se.await();
}
Thread.sleep(100);
arr[num]=new Random().nextInt(10);
System.out.println(Thread.currentThread().getName()+"second--"+"arr["+num+"]"+arr[num]);
flag=false;
conditon_fr.signal();
}
catch(Exception e)
{
}
finally
{
lock.unlock();
}
}
}
复制代码
作者:
addone
时间:
2016-8-25 23:52
public class NewTest4 {
public static void main(String[] args) {
ResourceType r = new ResourceType();
ProducerType pro = new ProducerType(r);
ConsumerType con = new ConsumerType(r);
new Thread(pro, "成产者线程").start();;
new Thread(con, "消费者线程").start();;
}
}
class ResourceType{
private int[] arr = new int[3];
private Random random = new Random();
private boolean flag;
Lock lock = new ReentrantLock();
Condition pro_condition = lock.newCondition();
Condition cons_condition = lock.newCondition();
public void set(){
lock.lock();
while(flag){
try {
pro_condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
contorlArr();
flag = true;
cons_condition.signal();
lock.unlock();
}
public void out(){
lock.lock();
while(!flag){
try {
cons_condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
contorlArr();
flag = false;
pro_condition.signal();
lock.unlock();
}
public void contorlArr(){
arr[0]=random.nextInt();
arr[1]=random.nextInt();
arr[2]=random.nextInt();
System.out.println(Thread.currentThread().getName()+"...#####...."+Arrays.toString(arr));
}
}
class ProducerType implements Runnable{
private ResourceType r;
public ProducerType(ResourceType r){
this.r = r;
}
@Override
public void run() {
while(true)
r.set();
}
}
class ConsumerType implements Runnable{
private ResourceType r;
public ConsumerType(ResourceType r){
this.r = r;
}
@Override
public void run() {
while(true)
r.out();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2