public class RunnableDemo implements Runnable
{
private int a;
@Override
public void run()
{
for (; a < 9; a++)
{
System.out.println(Thread.currentThread().getName() + " " +a );
}
}
public static void main(String[] args)
{
for (int i = 0; i < 9; i++)
{
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 4)
{
RunnableDemo runnable = new RunnableDemo();
new Thread(runnable,"新线程1:").start();
new Thread(runnable,"新线程2:").start();
}
}
}
}
结果是
main 0
main 1
main 2
main 3
main 4
main 5
新线程1: 0
main 6
新线程1: 1
新线程2: 1
main 7
新线程2: 3
新线程1: 2
新线程1: 5
新线程2: 4
main 8
新线程2: 7
新线程1: 6
新线程2: 8
------------------------------------------------------------------------------------------
以上过程并不能表示实现了runnable接口 线程间就能共享成员变量(很多书上写的都是实现这个接口就能共享成员变量),求高手解答 我很久没弄出来了,谢谢