public class A{
public int[] arrays=new int[10];
public synchronized void method1(int i,int off){
arrays[off]=i;
}
public synchronized int method2(int i){
return arrays[i];
}}class B implements Runnable{
public A a=null;
public int i,off;
public B(){};
public B(A arg,int off){
a = arg;
this.off=off;
}
public void run(){
a.method2(off);
}
}public class C implements Runnable{
public A a=null;
int i = 0;
public C(){}
public C(A arg,int j){
a = arg;
i = j;
}
public void run(){
System.out.println(a.method2(0));
}
}public class TestArrayThread
{
public static void main(String[] args) {
A a=new A();
B b=new B(a,9);
C c=new C(a,0,11);
Thread t = new Thread(b);
t.start();
t = new Thread(c);
t.start();
}
};这样写的算不算线程的呢?
public int[] arrays=new int[10];
public synchronized void method1(int i,int off){
arrays[off]=i;
}
public synchronized int method2(int i){
return arrays[i];
}}class B implements Runnable{
public A a=null;
public int i,off;
public B(){};
public B(A arg,int off){
a = arg;
this.off=off;
}
public void run(){
a.method2(off);
}
}public class C implements Runnable{
public A a=null;
int i = 0;
public C(){}
public C(A arg,int j){
a = arg;
i = j;
}
public void run(){
System.out.println(a.method2(0));
}
}public class TestArrayThread
{
public static void main(String[] args) {
A a=new A();
B b=new B(a,9);
C c=new C(a,0,11);
Thread t = new Thread(b);
t.start();
t = new Thread(c);
t.start();
}
};这样写的算不算线程的呢?
public class testthread extends Object implements Runnable
{
int line[] = new int[10];
//线程一,覆盖run方法实现读数据A的方法
public void run()
{
int a = 1;
a = line[1];
System.out.println("A类方法1:用于读取数组line的第一个元素");
System.out.println("line[1] = " + a);
}
public static void main(String para[])
{
testthread obj = new testthread();
Thread t = new Thread(obj);
t.start();
//main thread runing
obj.loadData();
}
//主线程,用于将b写入最后一个元素
public void loadData()
{
int b = 10;
line[9] = b;
System.out.println("A类方法1:用于读取数组line的最后一个元素");
System.out.println("line[10] = " + b);
}
}没有向楼上的用了线程同步约束,你自己调试
import java.lang.*;
public class Testthread implements Runnable
{
int[] line = new int[10];
public Testthread()
{
for(int i=0;i<10;i++)
line[i]=i;
}
public void run()
{
int a;
while(true)
{
a = line[0];
System.out.println("line[0] = " + a);
}
}
public static void main(String [] para)
{
Testthread obj = new Testthread();
Thread t = new Thread(obj);
t.start();
obj.loadData();
}
public void loadData()
{
while(true)
{
System.out.println("line[9] = " +line[9]);
}
}
}