随便写几句,就是如果把里边的如storeLocation这些东东传出去??
(new Thread(){
public void run()
{
statics = new Integer[256];
for(int i=0;i<256;i++)
{
statics[i]=0;
} for(int i=0;i<256;i++)
{
if(max < statics[i])
{
max = statics[i];
storeLocation = i;
}
}
}
}).start();
(new Thread(){
public void run()
{
statics = new Integer[256];
for(int i=0;i<256;i++)
{
statics[i]=0;
} for(int i=0;i<256;i++)
{
if(max < statics[i])
{
max = statics[i];
storeLocation = i;
}
}
}
}).start();
解决方案 »
- 请问Java中如何实现两个线程同时读一个文件?
- 请问如何在swing上显示摄像头拍摄的图像
- 数据录入问题
- 我都纳闷了,为什么我的分就没人要呢???
- 请帮忙调试一个小毛病关于setHorizontalScrollBarPolicy的!
- [讨论]QQ聊天文字与图片的传输、保存和显示.JAVA如何编程.参与有分
- 远程发送给server多个class文件,server怎么执行这些class?
- 高分求解!redhat linux7.2+jdk1.4的中文显示问题.
- 想学JAVA,什么都不懂!先有这些问题!
- 如何让Applet在浏览器中运行
- SCJP的一道考题,求解释
- 软件包sun.jdbc不存在是怎么回事啊?
public static void main(String args[]) {
System.out.println(new I(){
private int i=1;
public void foo(){}
public int getI(){return i;}
}.getI());
}
}interface I{
void foo();
}
这种内部类很大程度上是为了书写方便,或者是为了更好的封装,体现逻辑结构。
如果你又要让你的 thread start,又要返回其他的值,这种复杂的时候就不要考虑用内部类了
传给内部类,你在内部类中往里加元素。外部就能得到。或者直接声明一个元素的数组来代替你要声明的变量。比如原来你想声明一个Date变量,在内部类中想改变它,但因为final的原因你不能改变,你可以:final Date[] d = new Date[1];
d[0] = new Date();然后你在外部或内部类中用d[0]来代表要传入的Date,你就可以任意修改。
Callable接口和Future接口 就可以简单实现你那个功能(new ExecutorService().submint(new Callable({
public Object call() throws Exception
{
statics = new Integer[256];
for(int i=0;i<256;i++)
{
statics[i]=0;
} for(int i=0;i<256;i++)
{
if(max < statics[i])
{
max = statics[i];
storeLocation = i;
}
}
return storeLocation;
}
}));
//返回的是Future值
//通过Future.get() 就可以简单的得到线程返回的Object
public class Test
{
public static void main(String[] args)
{
final int account[]=new int[1];
System.out.println("之前:"+account[0]);
new ClassTest(){
public void addLocalVaribal(){
account[0]++;
}
}.addLocalVaribal();
System.out.println("之后:"+account[0]);
}
}
class ClassTest{
}LZ看看是不是这种效果,利用数组解决。
public static void main(String[] args) {
TT tt = new Test().new TT();
System.out.println(tt.getX());
}
class TT {
final int x=0;
int getX() {return x;}
}
}
(new Thread(){
public void run()
{
statics = new Integer[256];
for(int i=0;i<256;i++)
{
statics[i]=0;
} for(int i=0;i<256;i++)
{
if(max < statics[i])
{
max = statics[i];
storeLocation = i;//我敢保证,即使你把外部读storeLocation放在这段代码后面,
//读取顺序依然是相反的,先读了旧的storelocation,然后它才被更新的。
//由于对于线程目前不熟练,所以还没办法高效地给你修改代码。
//我也此贴 }
}
}
}).start();
(new Thread(){
public void run()
{
statics = new Integer[256];
for(int i=0;i<256;i++)
{
statics[i]=0;
} for(int i=0;i<256;i++)
{
if(max < statics[i])
{
max = statics[i];
storeLocation = i;//我敢保证,即使你把外部读storeLocation放在这段代码后面,
//读取顺序依然是相反的,先读了旧的storelocation,然后它才被更新的。
//由于对于线程目前不熟练,所以还没办法高效地给你修改代码。
//我也此贴 }
}
}
}).start();
public void run()
{
statics = new Integer[256];
for(int i=0;i<256;i++)
{
statics[i]=0;
} for(int i=0;i<256;i++)
{
if(max < statics[i])
{
max = statics[i];
storeLocation = i;
//我敢保证,即使你把外部读storeLocation放在这段代码后面,
//读取顺序依然是相反的,先读了旧的storelocation,然后它才被更新的。
//由于对于线程目前不熟练,所以还没办法高效地给你修改代码。
//我也此贴 }
}
}
}).start();