日期:2014-05-20  浏览次数:20962 次

我想自杀?求大神解惑
Thread.currentThread ().getName 运行得到:Thread-0
Thread.currentThread () 运行得到:Thread[Thread-0,5,main]
.............

1问:Java里分类名和对象名,上面的哪个是线程的对象名犹如在AA a = new AA ()里AA是类名而a是对象名,api文档说的我咋感觉说的那么含糊都是对象,我的理解是线程的类名叫Thread,搞不清线程的对象名到底是什么样式的?上述得到的表示什么意思干嘛要用两种表示线程的对象名称?

.......................................................

2问:还是例如AA a = new AA(),我调用object类的toString()方法,用打印System,out.println (a)的到a@.......;后面干嘛要带个哈希值,我api说返回对象的字符串表示,那我的理解不就单单打印输出a不就好了干嘛带个尾巴,但是我又这样去理解,既然打印就是把栈内存a存放的地址给转换为字符串那又内存中何来的a.

3问编译的文件名.class文件这里面是1010101这样的二进制文件对吗,为什么各个语言编译出来的文件后缀都不一样呢,怎么被机器识别啊?还是java有虚拟机乍滴???我咋越学越想知道最底层的东西。谢谢
.....................................................
[code=Java][/code]import java.util.concurrent.locks .*;

class kuang
{
private int [] xx = new int [100];
private int putptr=0;
private int takeptr=0;
private int count=0 ;
private Lock lock = new ReentrantLock ();
private Condition notfull = lock.newCondition();
private Condition notempty = lock.newCondition ();

public void add (int x)
{
lock.lock();
try
{

while (count==xx.length)
{
notempty.await();

}
xx[putptr]=x;

System.out.println("生产馒头"+xx[putptr++]);
count++;
notfull.signal();
}
catch(InterruptedException e)
{
return;
}
finally
{
lock.unlock();
}


}
public void get ()
{
lock.lock();
try
{
while (count==0)
{
notfull.await ();

}

System.out.println ("消费馒头"+xx[takeptr++]);
count--;
notempty.signal ();

}
catch (InterruptedException e)
{
return;
}
finally
{
lock.unlock();
}


}
}
class producer implements Runnable
{
private kuang res;
public producer (kuang res)
{
this.res = res;
}
public void run ()
{

for (int j=0;j<100;j++)
{
res.add (j);
}

}
}
class conducer implements Runnable
{
private kuang res;
public conducer (kuang res)
{
this.res = res;
}
public void run ()
{
while (true)
{
res.get ();
}
}
}
class ThreadTest2
{
public static void main (String [] args)
{
kuang aa = new kuang ();
producer bb = new producer (aa);
conducer cc = new conducer (aa);
Thread t1 = new Thread (bb);
Thread t2 = new Thread (bb);
// Thread t3 = new Thread (bb);
Thread t4 = new Thread (cc);
Thread t5= new Thread (cc);
t1.start();
t2.start();
// t3.start();
t4.start();
t5.start ();

}
}
我想实现先往数组里放100个然后再全部挨个打印出来,本来想搞3个生产线程,两个消费线程发现咋存到80就往外打印还有许多异常,就是上面删除注释的效果。然后我找原因我就消费生产都搞两个线程,结果接近了但是最后还是数组越界。。。。我咋设计线程这么困难啊。你门是不是写线程就和写对象一样简单还不会出错,我是出错了还得慢慢想啊想,尼玛我的要死啊。

------解决方案--------------------
问题3:
class文件里面保存的不是二进制内容的,只是java虚拟机可以识别的
------解决方案--------------------
你那两个Condition名字有点混淆,最好对调下,虽然没什么大碍,还有你的两个标志putptr和takeptr一直在自增,肯定会越界。最好是都对100取宇,putptr%100.产品池空和满的状态判断可以用:空:putptr=takeptr 满:(putptr+1)%100 = takeptr。 其他还有很多地方需要改动的,你自己在看看
------解决方案--------------------
在你的思路上,我帮你做了一个小小的改动,你试试

import java.util.concurrent.locks.*;

class kuang {
private int[] xx = new int[100];
private int putptr = 0;
private int takeptr = 0;
// private transient int count = 0;
private Lock lock = new ReentrantLock();
private Condition ne = lock.newCondition();
private Condition nf = lock.newCondition();