多线程共享实例变量例子
在网上查了资料,说类的实例变量是多线程共享的。经过实验为什么不是呢?请看下面的代码
package test;
public class ThreadDemo extends Thread{
private int i = 0;
@Override
public void run() {
System.out.println(this +":" + ++i);
}
public static void main(String[] args){
for(int i=0;i<5;i++){
ThreadDemo t = new ThreadDemo();
t.start();
}
}
}
上面的代码我生成了五个线程。每个线程启动的时候,把实例变量+1然后打印,就得到了下面的结果
Thread[Thread-0,5,main]:1
Thread[Thread-1,5,main]:1
Thread[Thread-2,5,main]:1
Thread[Thread-3,5,main]:1
Thread[Thread-4,5,main]:1
从这里看,好像每个线程都独自拥有一个变量i。而变量i并没有在多个线程之间共享。这是为什么呢?
public class Test {
public static void main(String[] args) throws Exception {
Runnable r1 = new A();
Thread t1 = new Thread(r1,"t1");
Thread t2 = new Thread(r1,"t2");
t1.start();
t2.start();
}
}
class A implements Runnable{
private int i = 0;
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+i++);
}
}
这样能明白点?你的每个thread都生成新对象,放在新的地址里,怎么共享?
# include<stdio.h>
# include<pthread.h>
# include<semaphore.h>
# define N 2
void *thread(void *arg);
sem_t mutex;
char **ptr;
int main(int argc,char *argv[])
{
int i;
pthread_t tid;
char *msgs[N]={"hello","world"}; //msgs本地自动变量,存放在线程自己的栈中,但同样可以被本进程的其余线程读写
ptr=msgs;
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
pthread_create(&tid,NULL,thread,(void *)i);
pthread_exit(NULL);
}
void *thread(void *arg)
{
int myid=(int)arg;
static int cnt=0; //cnt只有一个实例,被进程中的所有线程共享
pthread_detach(pthread_self());
sem_wait(&mutex); //如果没有信号量mutex,将N的值增大,cnt又是怎样(一定等于N值吗)?
printf("[%d], %s (cnt=%d)\n",myid,ptr[myid],++cnt);
sem_post(&mutex);
}
在网上查了资料,说类的实例变量是多线程共享的。经过实验为什么不是呢?请看下面的代码
package test;
public class ThreadDemo extends Thread{
private int i = 0;
@Override
public void run() {
System.out.println(this +":" + ++i);
}
public static void main(String[] args){
for(int i=0;i<5;i++){
ThreadDemo t = new ThreadDemo();
t.start();
}
}
}
上面的代码我生成了五个线程。每个线程启动的时候,把实例变量+1然后打印,就得到了下面的结果
Thread[Thread-0,5,main]:1
Thread[Thread-1,5,main]:1
Thread[Thread-2,5,main]:1
Thread[Thread-3,5,main]:1
Thread[Thread-4,5,main]:1
从这里看,好像每个线程都独自拥有一个变量i。而变量i并没有在多个线程之间共享。这是为什么呢?
public class Test {
public static void main(String[] args) throws Exception {
Runnable r1 = new A();
Thread t1 = new Thread(r1,"t1");
Thread t2 = new Thread(r1,"t2");
t1.start();
t2.start();
}
}
class A implements Runnable{
private int i = 0;
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+i++);
}
}
这样能明白点?你的每个thread都生成新对象,放在新的地址里,怎么共享?
# include<stdio.h>
# include<pthread.h>
# include<semaphore.h>
# define N 2
void *thread(void *arg);
sem_t mutex;
char **ptr;
int main(int argc,char *argv[])
{
int i;
pthread_t tid;
char *msgs[N]={"hello","world"}; //msgs本地自动变量,存放在线程自己的栈中,但同样可以被本进程的其余线程读写
ptr=msgs;
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
pthread_create(&tid,NULL,thread,(void *)i);
pthread_exit(NULL);
}
void *thread(void *arg)
{
int myid=(int)arg;
static int cnt=0; //cnt只有一个实例,被进程中的所有线程共享
pthread_detach(pthread_self());
sem_wait(&mutex); //如果没有信号量mutex,将N的值增大,cnt又是怎样(一定等于N值吗)?
printf("[%d], %s (cnt=%d)\n",myid,ptr[myid],++cnt);
sem_post(&mutex);
}