linux多线程访问共享资源,多个线程访问共享对象和数据

多个线程访问共享对象和数据有很多方式,但是可以分为两大类:1.多线程执行代码相同的。2.多线程执行代码不同的

对于多线程执行代码相同的情况来说:

1)使用同一个Runnable对象,在new Thread的时候放入相同的这个对象

public static void main(String[] args) {

Runnable r = new Runnable() {

int num = 100;

@Override

public synchronized void run() {

System.out.println(++num + "==add");

}

};

Thread t1 = new Thread(r);

Thread t2 = new Thread(r);

Thread t3 = new Thread(r);

t1.start();

t2.start();

t3.start();

}

对于多线程代码不同的情况来说:

1)将需要共享的对象或者数据放入另一个对象中,这个对象对外提供对其操作的方法,由不同的线程调用。

class Number {

int num = 100;

public int add() {

return ++num;

}

public int del() {

return --num;

}

}

Runnable对象中使用同一个Number对象

public static void main(String[] args) {

final Number num = new Number();

Runnable rAdd = new Runnable() {

@Override

public synchronized void run() {

System.out.println(num.add() + "==add");

}

};

Runnable rDel = new Runnable() {

@Override

public synchronized void run() {

System.out.println(num.del() + "==del");

}

};

Thread t1 = new Thread(rAdd);

Thread t2 = new Thread(rDel);

Thread t3 = new Thread(rAdd);

t1.start();

t2.start();

t3.start();

}

2)将Runnable写成内部类的形式,然后将需要共享的对象和数据写成外部类的成员变量。

class Number{

int num = 100;

Runnable r_add = new Runnable() {

@Override

public synchronized void run() {

System.out.println(++num+"==add");

}

};

Runnable r_del = new Runnable() {

@Override

public synchronized void  run() {

System.out.println(--num+"==del");

}

};

}

然后通过调用Number对象中的内部类对象来实现。

public static void main(String[] args) {

Number n = new Number();

Thread t1 = new Thread(n.r_add);

Thread t2 = new Thread(n.r_del);

t1.start();

t2.start();

}

------------------------------------------------------------------------------

还有一种方法简单粗暴,就是把需要共享的对象和数据用static修饰。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例程序,展示了如何在多线程中实现回调函数访问共享资源数据同步: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> typedef struct { int value; pthread_mutex_t mutex; pthread_cond_t cond; } shared_data_t; void* thread_func(void* arg); void callback_func(void* arg) { shared_data_t* shared_data = (shared_data_t*) arg; pthread_mutex_lock(&shared_data->mutex); shared_data->value++; pthread_cond_signal(&shared_data->cond); pthread_mutex_unlock(&shared_data->mutex); } int main() { shared_data_t shared_data = {0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER}; pthread_t thread; if (pthread_create(&thread, NULL, thread_func, (void*) &shared_data) != 0) { fprintf(stderr, "Error creating thread.\n"); exit(1); } pthread_mutex_lock(&shared_data.mutex); while (shared_data.value == 0) { pthread_cond_wait(&shared_data.cond, &shared_data.mutex); } printf("Value updated by callback: %d\n", shared_data.value); pthread_mutex_unlock(&shared_data.mutex); pthread_join(thread, NULL); return 0; } void* thread_func(void* arg) { shared_data_t* shared_data = (shared_data_t*) arg; // Do some work... callback_func(shared_data); return NULL; } ``` 在这个程序中,我们定义了一个 `shared_data_t` 结构体,其中包含一个整数值 `value`,一个互斥锁 `mutex` 和一个条件变量 `cond`。我们使用互斥锁来保护共享资源 `value` 的访问,使用条件变量来实现线程之间的同步。 在 `thread_func` 函数中,我们调用了回调函数 `callback_func`,并将共享数据结构体的指针作为参数传递给它。在 `callback_func` 中,我们先使用互斥锁锁定共享数据结构体,然后对 `value` 进行更新,并使用条件变量通知其他等待线程。最后,我们释放互斥锁。 在 `main` 函数中,我们启动一个新的线程,并等待回调函数更新共享数据。我们使用条件变量来等待线程通知,如果 `value` 的值仍然为 0,则线程将等待条件变量的信号。一旦条件变量被通知,我们再次锁定互斥锁来读取更新后的值,并释放互斥锁。 这个示例程序展示了如何在多线程中实现回调函数访问共享资源数据同步的基本原理。当然,实际情况可能更加复杂,需要更多的同步机制来确保线程安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值