class AThread implements Runnable
{
private int x=0;
public void run()
{
while(true)
{
for(int i=0;i<3;i++)
{
System.out.print("* ");
}
System.out.println();
try{Thread.sleep(1000);}catch(Exception e){}
}
}
}
class BThread implements Runnable
{
public void run()
{
while(true)
{
System.out.println("& ");
try{Thread.sleep(100);}catch(Exception e){}
}
}
}
class CThread implements Runnable
{
public void run()
{
while(true)
{
System.out.println("$ ");
try{Thread.sleep(500);}catch(Exception e){}
}
}
}
class PrintThread
{
public static void main(String [] args)
{
//String str=new String("");
new Thread(new AThread()).start();
new Thread(new BThread()).start();
new Thread(new CThread()).start();
}
}
输出的结果为:
* * *
&
$
$
$
$
$
*&
* *
我想这应该是线程没有同步的问题.如果我想使用synchronized(str){}我应该在哪创建一个str对象呢.同一个对象?
{
private int x=0;
public void run()
{
while(true)
{
for(int i=0;i<3;i++)
{
System.out.print("* ");
}
System.out.println();
try{Thread.sleep(1000);}catch(Exception e){}
}
}
}
class BThread implements Runnable
{
public void run()
{
while(true)
{
System.out.println("& ");
try{Thread.sleep(100);}catch(Exception e){}
}
}
}
class CThread implements Runnable
{
public void run()
{
while(true)
{
System.out.println("$ ");
try{Thread.sleep(500);}catch(Exception e){}
}
}
}
class PrintThread
{
public static void main(String [] args)
{
//String str=new String("");
new Thread(new AThread()).start();
new Thread(new BThread()).start();
new Thread(new CThread()).start();
}
}
输出的结果为:
* * *
&
$
$
$
$
$
*&
* *
我想这应该是线程没有同步的问题.如果我想使用synchronized(str){}我应该在哪创建一个str对象呢.同一个对象?
Object lock = new Object();
//String str=new String("");
new Thread(new AThread(lock)).start();
new Thread(new BThread(lock)).start();
new Thread(new CThread(lock)).start();
}
}class AThread implements Runnable {
private Object lock;
public AThread(Object lock) {
this.lock = lock;
}
private int x = 0;
public void run() {
while (true) {
synchronized (lock) {
for (int i = 0; i < 3; i++) {
System.out.print("* ");
}
System.out.println();
try {
Thread.sleep(1000);
}
catch (Exception e) {}
}
}
}
}class BThread implements Runnable {
private Object lock;
public BThread(Object lock) {
this.lock = lock;
}
public void run() {
while (true) {
synchronized (lock) {
System.out.println("& ");
try {
Thread.sleep(100);
}
catch (Exception e) {}
}
}
}
}class CThread implements Runnable {
private Object lock;
public CThread(Object lock) {
this.lock = lock;
}
public void run() {
while (true) {
synchronized (lock) {
System.out.println("$ ");
try {
Thread.sleep(500);
}
catch (Exception e) {}
}
}
}
}
private int x=0;
public void run() {
for(int i=0;i<10;i++){
try{Thread.sleep(50);}catch(Exception e){}
System.out.print("* ");
}
System.out.println();
}
}
class BThread implements Runnable{
// AThread a = new AThread(); public void run() {
//synchronized(AThread.class) {
for(int i=0;i<10;i++){
try{Thread.sleep(50);}catch(Exception e){}
System.out.print("& ");
}
//}
System.out.println();
}
}
class CThread implements Runnable{
public void run(){
for(int i=0;i<10;i++){
try{Thread.sleep(50);}catch(Exception e){}
System.out.print("$ ");
}
System.out.println();
}
}
class PrintThread{
public static void main(String [] args) throws Exception {
Thread a = new Thread(new AThread());
a.start();
a.join();
Thread b = new Thread(new BThread());
b.start();
b.join();
Thread c = new Thread(new CThread());
c.start();
c.join();
}
}
blackpark(暖咖啡) 你有没有测过你的程序亚
运行结果:
* * * * * * * * * *
& & & & & & & & & &
$ $ $ $ $ $ $ $ $ $
程序结束