📄 j_thread.java.bak
字号:
// ////////////////////////////////////////////////////////
//
// J_Thread.java
//
// Created by Jun-Hai Yong, on XX xx, xxxx (Date)
// ////////////////////////////////////////////////////////
// Readme:
// Demonstrating the Thread class.
// ////////////////////////////////////////////////////////
// Using this example, please explicitly refer to the book:
// Jun-Hai Yong. Programming in Java.
// Beijing: Tsinghua University Press, 2004.
// 使用本例子,请注明引用:
// 雍俊海. Java 程序设计. 北京: 清华大学出版社, 2004.
// Please note that the author and publisher make no
// warranty of any kind on the examples provided.
// ////////////////////////////////////////////////////////
public class J_Thread extends Thread
{
private int m_sleepTime;
public J_Thread(String name, int t)
{
super(name);
m_sleepTime=t;
} // End of method: J_Thread
public void loop( )
{
for(int i=0; i<1000; i++)
{
double d= Math.sin(i);
int j= (int)d+i;
i= j-(int)d;
}
} // End of method: loop
public void run( )
{
System.out.println(getName( ) + " begins running.");
int k= 2;
for(int i=0; i<k; i++)
{
System.out.println(getName( ) + " : " + i + " with priority " + getPriority( ));
for(int j=0; j<1000; j++)
loop( );
} // End of outer for loop
try
{
sleep(m_sleepTime*1000);
}
catch(InterruptedException e)
{
}
for(int i=k; i<k+k; i++)
{
System.out.println(getName( ) + " : " + i + " with priority " + getPriority( ));
for(int j=0; j<1000; j++)
loop( );
} // End of outer for loop
System.out.println(getName( ) + " stops running.");
} // End of method: run
public static void main( String args[] )
{
J_Thread frank = new J_Thread("Frank", 1);
J_Thread mary = new J_Thread("Mary", 2);
J_Thread alice = new J_Thread("alice", 2);
J_Thread chris = new J_Thread("Chris", 1);
frank.setPriority(Thread.MIN_PRIORITY);
mary.setPriority(Thread.NORM_PRIORITY);
alice.setPriority(Thread.NORM_PRIORITY);
chris.setPriority(Thread.MAX_PRIORITY);
frank.start( );
mary.start( );
alice.start( );
chris.start( );
} // End of method: main
} // End of class: J_Thread
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -