我正在用java编写代码来打印输出,如下所示
[精神]
[的]
[维普罗]
但是我在设置线程的优先级并查看每个线程的优先级并且没有按预期获得输出时遇到了问题。
class shobj
{
public synchronized void sharedMethod(String arg)
{
System.out.print("[");
System.out.print(arg);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println("INTERRUPTED");
}
System.out.println("]");
}
}
class thread1 implements Runnable
{
String arg;
shobj obj1;
Thread t;
public thread1(shobj obj1,String arg)
{
this.obj1=obj1;
this.arg=arg;
t=new Thread(this);
t.start();
// System.out.println(t.currentThread());
}
public void run()
{
obj1.sharedMethod(arg);
}
}
class synchro
{
public static void main(String args[])
{
shobj ob = new shobj();
thread1 x1 = new thread1(ob,"spirit");
thread1 x2 = new thread1(ob,"of");
thread1 x3 = new thread1(ob,"wipro");
x3.t.setPriority(Thread.NORM_PRIORITY+3);
x2.t.setPriority(Thread.NORM_PRIORITY+2);
x1.t.setPriority(Thread.NORM_PRIORITY+1);
try
{
x1.t.join(); //System.out.println(x1.t.currentThread());
x2.t.join();//System.out.println(x2.t.currentThread());
x3.t.join();//System.out.println(x3.t.currentThread());
}
catch(Exception e)
{
System.out.println("Interruted Exception");
}
}
}
我得到如下输出:
[精神]
[维普罗]
[的]