java多线程轮替(在线等,急)
在main()中开辟两个线程。如何让这两个线程严格按照Thread-1Thread-2Thread-1Thread-2...这样每个线程分别执行一次?不胜感谢...
在main()中开辟两个线程。如何让这两个线程严格按照
Thread-1
Thread-2
Thread-1
Thread-2
...
这样每个线程分别执行一次?
不胜感谢 展开
Thread-1
Thread-2
Thread-1
Thread-2
...
这样每个线程分别执行一次?
不胜感谢 展开
5个回答
展开全部
package com.koolbao.test;
public class CopyOfTestThread {
private Thread thread1;
private Thread thread2;
private boolean flag = false;// 公共变量
public static void main(String[] args) {
CopyOfTestThread t = new CopyOfTestThread();
t.createTread1();
t.createTread2();
}
public void createTread1() {// 程序线程
thread1 = new Thread(new ThreadI());
thread1.start();
}
public void createTread2() {// 线程II
thread2 = new Thread(new ThreadII());
thread2.start();
}
class ThreadI implements Runnable {
private int temp = 0;
public void run() {
while (true) {
if (!flag) {
System.out.println("这里放ThreadI要做的代码");
flag = true;// 更改公共变量
}
try {// 这里只是为了观察方便可以删除
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ThreadII implements Runnable {
public void run() {
while (true) {
if (flag) {
System.out.println("这里放ThreadII要做的代码");
flag = false;// 更改公共变量
}
try {// 这里只是为了观察方便可以删除
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
你说的是不是这样的结果
public class CopyOfTestThread {
private Thread thread1;
private Thread thread2;
private boolean flag = false;// 公共变量
public static void main(String[] args) {
CopyOfTestThread t = new CopyOfTestThread();
t.createTread1();
t.createTread2();
}
public void createTread1() {// 程序线程
thread1 = new Thread(new ThreadI());
thread1.start();
}
public void createTread2() {// 线程II
thread2 = new Thread(new ThreadII());
thread2.start();
}
class ThreadI implements Runnable {
private int temp = 0;
public void run() {
while (true) {
if (!flag) {
System.out.println("这里放ThreadI要做的代码");
flag = true;// 更改公共变量
}
try {// 这里只是为了观察方便可以删除
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ThreadII implements Runnable {
public void run() {
while (true) {
if (flag) {
System.out.println("这里放ThreadII要做的代码");
flag = false;// 更改公共变量
}
try {// 这里只是为了观察方便可以删除
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
你说的是不是这样的结果
展开全部
貌似.....wait-notify吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有两种方法,只说思路 你自己试试
1,设置线程的优先级,
2,设置睡眠时间,2的睡眠时间小于1的
可以两种方法同时使用。
1,设置线程的优先级,
2,设置睡眠时间,2的睡眠时间小于1的
可以两种方法同时使用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置优先级和睡眠时间还是会有线程争抢到多次CPU时间片的微弱几率
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
模拟存取款,存完款马上取款,存款线程和取款线程交替执行。总工有2个public类
import account.Account;
public class TestSynchronized3 {
public static void main(String[] args) {
Account account = new Account("820915", 1000);
DrawThread dt = new DrawThread(account, 200);
new Thread(dt, "张三").start();
new Thread(dt, "张三的儿子").start();
}
}
class DrawThread implements Runnable {
private Account account;
private double drawAmount;
public DrawThread(Account account, double drawAccount){
this.account = account;
this.drawAmount = drawAmount;
}
public void run() {
account.drawMoney(drawAmount);
}
}
package account;
public class Account {
private String accountNO;
private double balance;
public Account(String accountNO, double balance){
this.accountNO = accountNO;
this.balance = balance;
}
public void setAccountNO(String accountNO){
this.accountNO = accountNO;
}
public String getAccountNO(){
return accountNO;
}
public double getbalance(){
return balance;
}
public synchronized void drawMoney(double drawAccount){
if(drawAccount <= balance){
System.out.println(Thread.currentThread().getName()+ "取钱" + drawAccount);
try {
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
balance = balance - drawAccount;
System.out.println("余额为:" + balance);
}
else {
System.out.println("余额不足,无法取钱!您的帐户上有金额" + balance);
}
}
}
import account.Account;
public class TestSynchronized3 {
public static void main(String[] args) {
Account account = new Account("820915", 1000);
DrawThread dt = new DrawThread(account, 200);
new Thread(dt, "张三").start();
new Thread(dt, "张三的儿子").start();
}
}
class DrawThread implements Runnable {
private Account account;
private double drawAmount;
public DrawThread(Account account, double drawAccount){
this.account = account;
this.drawAmount = drawAmount;
}
public void run() {
account.drawMoney(drawAmount);
}
}
package account;
public class Account {
private String accountNO;
private double balance;
public Account(String accountNO, double balance){
this.accountNO = accountNO;
this.balance = balance;
}
public void setAccountNO(String accountNO){
this.accountNO = accountNO;
}
public String getAccountNO(){
return accountNO;
}
public double getbalance(){
return balance;
}
public synchronized void drawMoney(double drawAccount){
if(drawAccount <= balance){
System.out.println(Thread.currentThread().getName()+ "取钱" + drawAccount);
try {
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
balance = balance - drawAccount;
System.out.println("余额为:" + balance);
}
else {
System.out.println("余额不足,无法取钱!您的帐户上有金额" + balance);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询