锁定老帖子 主题:三个线程循环打印abc十次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2011-12-27
最后修改:2011-12-27
Sev7en_jun 写道 多线程不是用来解决不需要按照顺序执行的任务吗,为什么要保证顺序 没有为什么,题目要求就是打印abc,abc,abc。题目是考察线程协作的 |
|
返回顶楼 | |
发表时间:2011-12-27
既然题目没有明确必须使用wait\notify等,可以不考虑线程间的协作,从另一个角度保证打印顺序。
|
|
返回顶楼 | |
发表时间:2011-12-27
my_queen 写道 既然题目没有明确必须使用wait\notify等,可以不考虑线程间的协作,从另一个角度保证打印顺序。
package king; /** * * @author king * 不使用线程协作 * */ public class PrintRun implements Runnable{ public String[] printCon = {"a","b","c"}; public int num = 0; public String getCon(){ return printCon[num%3]; } public synchronized void print(){ System.out.print(getCon()); num++; } public static void main(String[] args) throws InterruptedException{ PrintRun p = new PrintRun(); Thread p1 = new Thread(p); Thread p2 = new Thread(p); Thread p3 = new Thread(p); p1.start(); p2.start(); p3.start(); } @Override public void run() { for(int i=0;i<10;i++){ print(); } } } |
|
返回顶楼 | |
发表时间:2011-12-27
my_queen 写道 既然题目没有明确必须使用wait\notify等,可以不考虑线程间的协作,从另一个角度保证打印顺序。
是的,代码没问题。可能是我表述有问题,原题应该是:“ 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印” 所以我说是考察线程间的协作,呵呵,表述有误 |
|
返回顶楼 | |
发表时间:2011-12-27
用锁不行嘛?
public class Test extends Thread { public static void main(String[] args) { new Test('A', 0).start(); new Test('B', 1).start(); new Test('C', 2).start(); } private char ch; private static int times = 0; private static int state = 0; private int order; private Object lock = new Object(); public Test(char ch, int order) { this.ch = ch; this.order = order; } @Override public void run() { while(true) { synchronized (lock) { if(times > 29) return; if(state == order) { System.out.print(ch); state = (++state)%3; times++; } } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
|
返回顶楼 | |
发表时间:2011-12-28
syx278250658 写道 用锁不行嘛?
public class Test extends Thread { public static void main(String[] args) { new Test('A', 0).start(); new Test('B', 1).start(); new Test('C', 2).start(); } private char ch; private static int times = 0; private static int state = 0; private int order; private Object lock = new Object(); public Test(char ch, int order) { this.ch = ch; this.order = order; } @Override public void run() { while(true) { synchronized (lock) { if(times > 29) return; if(state == order) { System.out.print(ch); state = (++state)%3; times++; } } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } } 你的lock锁没有起到任何作用,呵呵。你是3个实例对象,3个线程,一个线程作用于一个实例锁是起不到作用的。 |
|
返回顶楼 | |
发表时间:2011-12-28
my_corner 写道 syx278250658 写道 用锁不行嘛?
public class Test extends Thread { public static void main(String[] args) { new Test('A', 0).start(); new Test('B', 1).start(); new Test('C', 2).start(); } private char ch; private static int times = 0; private static int state = 0; private int order; private Object lock = new Object(); public Test(char ch, int order) { this.ch = ch; this.order = order; } @Override public void run() { while(true) { synchronized (lock) { if(times > 29) return; if(state == order) { System.out.print(ch); state = (++state)%3; times++; } } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } } 你的lock锁没有起到任何作用,呵呵。你是3个实例对象,3个线程,一个线程作用于一个实例锁是起不到作用的。 +1 |
|
返回顶楼 | |
发表时间:2011-12-28
my_queen 写道 my_corner 写道 syx278250658 写道 用锁不行嘛?
public class Test extends Thread { public static void main(String[] args) { new Test('A', 0).start(); new Test('B', 1).start(); new Test('C', 2).start(); } private char ch; private static int times = 0; private static int state = 0; private int order; private Object lock = new Object(); public Test(char ch, int order) { this.ch = ch; this.order = order; } @Override public void run() { while(true) { synchronized (lock) { if(times > 29) return; if(state == order) { System.out.print(ch); state = (++state)%3; times++; } } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } } 你的lock锁没有起到任何作用,呵呵。你是3个实例对象,3个线程,一个线程作用于一个实例锁是起不到作用的。 +1 实现Runnable接口就可以了应该。 |
|
返回顶楼 | |
发表时间:2011-12-28
最后修改:2011-12-28
zc0604 写道 实现Runnable接口就可以了应该。
package king; public class PrintThread { public static void main(String args[]) throws InterruptedException{ Print pa = new Print("A",0); Print pb = new Print("B",1); Print pc = new Print("C",2); pa.start(); pb.start(); pc.start(); } } class Print extends Thread{ private static int[] num = {0}; //锁,同时记录打印次数 private int start; //线程第一次打印的位置 public Print(){ this("",-1); } public Print(String name,int start){ super(name); this.start = start; } public void run(){ for(int i=start;i<=27+start;i+=3){ synchronized(num){ if(num[0]==i){ System.out.print(Thread.currentThread().getName()); num[0]++; num.notifyAll(); } else{ try { i -= 3; num.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } |
|
返回顶楼 | |
发表时间:2011-12-28
我记得有一个类叫什Condition,它就是一个条件,你可以看下。应该能够很轻松地实现你要的功能。
java.util.concurent.locks里面。 |
|
返回顶楼 | |