如何让两个线程连续交替(严格交替)运行,不会出现一个线程执行几遍后才执行;另一个线程。我尝试使用yield(),sleep(),wait()和notify(),都不能实现这个要求。
解决方案 »
- jfreechart如何禁用默认的右键菜单并添加自己写的右键弹出菜单?
- 反射中的方法调用出现问题(invoke方法出了问题)
- winzip的问题还是java的中文问题?
- 请问如何在finally中得到前面try或catch中抛出的异常?
- 请问哪有jcreater和JDK下载?
- 请教:多点广播发生异常,IP用.0和.255都是一样的异常
- 一个基础的问题?
- 我想知道,微软如果不支持Java的话Jbuider还有发展前途吗?
- AWT中的定时器问题
- 求解 关于 初学者 出现空指针问题
- sqlserver 2005用ResultSetMetaData取列类型,日期取出了nvarchar
- 关于ArrayList集合放对方,然后怎么测算出其内存大小呢
用wait和notifyAll肯定能实现!
notifyall唤醒所有的等待线程那么执行顺序就乱了。
*
*/
package p1;/**
* @author Think
*
*/
public class Test {
public static void main(String[] args){
final Object lock = new Object();
Thread t1 = new T(lock), t2 = new T(lock); t1.start();
t2.start(); try {
Thread.currentThread().join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} static class T extends Thread{
private Object lock;
public T(Object lock){
this.lock = lock;
}
public void run(){
while(true){
synchronized(lock){
try {
System.out.println(this);
lock.notify();
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
第一个线程在X为偶数的时候执行,第二个线程在X为基数的时候执行,线程执行完之后把X++,然后唤醒等待L的所有线程;
public class Task {
private boolean falg=false;
public synchronized void taskNo1(){
falg=true;
notifyAll();
}
public synchronized void taskNo2(){
falg=false;
notifyAll();
}
public synchronized void waitForNo1() throws InterruptedException{
while(!falg)
wait();
}
public synchronized void waitForNo2() throws InterruptedException{
while(falg)
wait();
}}
import java.util.concurrent.TimeUnit;
public class No1 implements Runnable{
private Task task;
public No1(Task task){
this.task=task;
}
public void run() {
try{
while(!Thread.interrupted()){
System.out.println("task1!!!!!!");
TimeUnit.MILLISECONDS.sleep(200);
task.taskNo1();
task.waitForNo2();
}
}catch(InterruptedException e){
System.out.println(" exiting Interrupte");
}
}}
import java.util.concurrent.TimeUnit;
public class No2 implements Runnable{
private Task task;
public No2(Task task){
this.task=task;
}
public void run() {
try{
while(!Thread.interrupted()){
task.waitForNo1();
System.out.println("task2!!!!!");
TimeUnit.MILLISECONDS.sleep(200);
task.taskNo2();
}
}catch(InterruptedException e){
System.out.println(" exiting Interrupted");
}
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws InterruptedException {
Task task = new Task();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new No1(task));
exec.execute(new No2(task));
TimeUnit.SECONDS.sleep(5);
exec.shutdown();
}
}
public class ThreadABC extends Thread{
int i=0;
private static int count=0;
private static Object o=new Object(); public ThreadABC(String ID){
currentThread().setName(ID);
}
public void run() {
synchronized (o) {
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count%2==0&¤tThread().getName().equals("A")){
o.notify();
System.out.print(currentThread().getName());
count++;
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if (count%2==1&¤tThread().getName().equals("B")) {
o.notify();
System.out.print(currentThread().getName());
count++;
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}
}
public static void main(String[] args) {
ThreadABC b=new ThreadABC("B");
ThreadABC a=new ThreadABC("A");
a.setName("A");
b.setName("B");
a.start();
b.start();
}
}
看下我这个吧,两个线程交替地打印自己线程的名字
package com.haojia.test;public class State { public static void main(String[] args) {
Common c = new Common();
new Thread(new R1(c)).start();
new Thread(new R2(c)).start();
}
}class Common {
private Boolean state = true; public boolean isState() {
return state;
} public void setState(boolean state) {
this.state = state;
} public synchronized void r1() {
try {
while (state) {
wait();
}
System.out.println(Thread.currentThread().getName());
state = true;
notify(); } catch (InterruptedException e) {
e.printStackTrace();
}
} public synchronized void r2() {
try {
while (!state) {
wait();
}
System.out.println(Thread.currentThread().getName());
state = false;
notify(); } catch (InterruptedException e) {
e.printStackTrace();
}
}
}class R1 implements Runnable {
private Common common; public R1(Common common) {
this.common = common;
} @Override
public void run() {
for (int i = 0; i < 200; i++) {
common.r1();
}
}
}class R2 implements Runnable {
private Common common; public R2(Common common) {
this.common = common;
} @Override
public void run() {
for (int i = 0; i < 200; i++) {
common.r2();
}
}
}
while(flag==1){
.....方法体
flag=0;
}
while(flag==0){
.....方法体
flag=1;
}
}不理解我也没办法了`
设定一个两个线程的公共变量。线程启动时第一件事情就是去获得该变量的值,线程A只能在变量为单数时执行,线程B只能在变量为双数时执行,执行完毕后将该数字加一并notify。虽然AB线程会去竞争该变量,但是当A执行完以后如果继续获得变量的控制权,因为数字已经变了。所以A线程继续notify,直到B获取该变量,执行后加一才能轮到A继续执行。不过以上说的都是假设,notify的原则是先进先出,也就是当A notify 的时候理论上说一定是B更先进入wait列表,所以应该会是B先获得处理资格。反之亦然。