线程如何同步?
如何让下面的程序能输出
0
1
2
3
4
5
6
7
8
9帮我解决下
public class thread {
public thread() {
}
public static void main(String[] arg)
{
for(int i=0;i<10;i++)
{
tes hh=new tes();
hh.init(String.valueOf(i));
hh.go();
}
}
}
class tes extends Thread
{
String hh;
tes()
{}
public void init(String hh)
{
this.hh=hh;
}public void go()
{
start();
}
public void run()
{
synchronized(this)
{
staticclass.hh=this.hh;
try {
this.sleep(10000);
} catch (InterruptedException ex) { } System.out.print(staticclass.hh+"\n");
}
}
}
class staticclass {
static String hh="huhao";
public staticclass() {
}
}
如何让下面的程序能输出
0
1
2
3
4
5
6
7
8
9帮我解决下
public class thread {
public thread() {
}
public static void main(String[] arg)
{
for(int i=0;i<10;i++)
{
tes hh=new tes();
hh.init(String.valueOf(i));
hh.go();
}
}
}
class tes extends Thread
{
String hh;
tes()
{}
public void init(String hh)
{
this.hh=hh;
}public void go()
{
start();
}
public void run()
{
synchronized(this)
{
staticclass.hh=this.hh;
try {
this.sleep(10000);
} catch (InterruptedException ex) { } System.out.print(staticclass.hh+"\n");
}
}
}
class staticclass {
static String hh="huhao";
public staticclass() {
}
}
* $Id:$
*/
package answer;public class thread {
public thread() {
} public static void main(String[] arg) {
for (int i = 0; i < 10; i++) {
tes tes1 = new tes();
tes1.init(String.valueOf(i));
tes1.go();
}
}
}class tes extends Thread {
String hh; tes() {
} public void init(String hh) {
this.hh = hh;
} public void go() {
start();
} public void run() {
synchronized (staticclass.hh) {
try {
this.sleep(100);
staticclass.hh = this.hh;
} catch (InterruptedException ex) {
System.out.println(ex);
} System.out.print(staticclass.hh + "\n"); } }
}class staticclass {
static String hh = "huhao"; public staticclass() {
}
}
高哈
原来我没理解到synchronized (staticclass.hh)这个含义
能详细解释下吗?