62,620
社区成员
发帖
与我相关
我的任务
分享
class Student extends Thread {
private int age;
public Student(int age) {
this.age = age;
}
public synchronized void printAge(){
for(int i=0;i<10;i++){
System.err.println("age "+i+" is:"+age);
}
}
public synchronized void printAge(int age) {
//synchronized(this){
while (true){
System.out.println(age+"xxxxxxxxxxxxxxxxxxx");
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//}
}
public void run() {
printAge(age);
}
}
class LoggingWidget {
public static void main(String args[]) {
Student s1 = new Student(1);
s1.start();
s1.printAge();
}
}
class Student extends Thread {
private int age;
public Student(int age) {
this.age = age;
}
public synchronized void printAge() throws InterruptedException{
Thread.sleep(5000);
for(int i=0;i<10;i++){
System.err.println("age "+i+" is:"+age);
}
}
public synchronized void printAge(int age) {
//synchronized(this){
while (true){
System.out.println(age+"xxxxxxxxxxxxxxxxxxx");
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//}
}
public void run() {
printAge(age);
}
}
class Sample {
public static void main(String args[]) throws InterruptedException {
Student s1 = new Student(1);
//s1.start();
s1.printAge();
s1.start();
}
}
每次都会交叉输出
按照你说的 SYSTEM.ERR改成SYSTEM.OUT就不会交叉了 就是同步了。。[/quote]
还是有点不懂 虽然是不同的输出流 但是也都是在synchronized里面 应该不会造成不同步啊?[/quote]我先说的最后一点你测试过没有?你代码里的输出只能说是输出顺序,不代表就是执行顺序。同一个流不管怎样肯定会顺序打印,两个流会如何同时打印在一个控制台上那就不确定了。[/quote]
明白了 谢谢。
class Student extends Thread {
private int age;
public Student(int age) {
this.age = age;
}
public synchronized void printAge() throws InterruptedException{
Thread.sleep(5000);
for(int i=0;i<10;i++){
System.err.println("age "+i+" is:"+age);
}
}
public synchronized void printAge(int age) {
//synchronized(this){
while (true){
System.out.println(age+"xxxxxxxxxxxxxxxxxxx");
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//}
}
public void run() {
printAge(age);
}
}
class Sample {
public static void main(String args[]) throws InterruptedException {
Student s1 = new Student(1);
//s1.start();
s1.printAge();
s1.start();
}
}
每次都会交叉输出
按照你说的 SYSTEM.ERR改成SYSTEM.OUT就不会交叉了 就是同步了。。[/quote]
还是有点不懂 虽然是不同的输出流 但是也都是在synchronized里面 应该不会造成不同步啊?[/quote]我先说的最后一点你测试过没有?你代码里的输出只能说是输出顺序,不代表就是执行顺序。同一个流不管怎样肯定会顺序打印,两个流会如何同时打印在一个控制台上那就不确定了。
class Student extends Thread {
private int age;
public Student(int age) {
this.age = age;
}
public synchronized void printAge() throws InterruptedException{
Thread.sleep(5000);
for(int i=0;i<10;i++){
System.err.println("age "+i+" is:"+age);
}
}
public synchronized void printAge(int age) {
//synchronized(this){
while (true){
System.out.println(age+"xxxxxxxxxxxxxxxxxxx");
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//}
}
public void run() {
printAge(age);
}
}
class Sample {
public static void main(String args[]) throws InterruptedException {
Student s1 = new Student(1);
//s1.start();
s1.printAge();
s1.start();
}
}
每次都会交叉输出
按照你说的 SYSTEM.ERR改成SYSTEM.OUT就不会交叉了 就是同步了。。[/quote]
还是有点不懂 虽然是不同的输出流 但是也都是在synchronized里面 应该不会造成不同步啊?
class Student extends Thread {
private int age;
public Student(int age) {
this.age = age;
}
public synchronized void printAge() throws InterruptedException{
Thread.sleep(5000);
for(int i=0;i<10;i++){
System.err.println("age "+i+" is:"+age);
}
}
public synchronized void printAge(int age) {
//synchronized(this){
while (true){
System.out.println(age+"xxxxxxxxxxxxxxxxxxx");
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//}
}
public void run() {
printAge(age);
}
}
class Sample {
public static void main(String args[]) throws InterruptedException {
Student s1 = new Student(1);
//s1.start();
s1.printAge();
s1.start();
}
}
每次都会交叉输出
按照你说的 SYSTEM.ERR改成SYSTEM.OUT就不会交叉了 就是同步了。。class Student extends Thread {
private int age;
public Student(int age) {
this.age = age;
}
public synchronized void printAge(){
for(int i=0;i<10;i++){
System.err.println("age "+i+" is:"+age);
}
}
public synchronized void printAge(int age) {
//synchronized(this){
while (true){
System.out.println(age+"xxxxxxxxxxxxxxxxxxx");
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//}
}
public void run() {
printAge(age);
}
}
class Test {
public static void main(String args[]) {
Student s1 = new Student(1);
s1.start();
try{
Thread.sleep(5000);//主线程休眠,然s1线程执行
}catch (InterruptedException e) {
e.printStackTrace();
}
s1.printAge();
}
}
[/quote]
[/quote]
这个结果是正常的。
线程的状态有几种
创建,可运行,运行,睡眠(等待),死亡
s1.start();这个是可运行状态,但是程序不一定在运行,只是先挂起。由于这里时间特别短,所以CPU就有权先运s1.printAge();所以结果就会是你一开始的那种情况。如果照我改的去运行,时间片长了,就会先运行s1.start();这就不会有你说的那种情况了。