?? k.txt
字號:
----------------------k1----------------------
import java.io.*;
public class a // Application主類
{
public static void main(String args[]) //輸入一個整數參數(素數檢查上限)
{
if(args.length<1)
{
System.out.println("請輸入一個命令行參數");
System.exit(0);
}
//創建用戶Thread子類的對象實例,使其處于NewBorn狀態
primeThread getPrimes = new primeThread(Integer.parseInt(args[0]));
getPrimes.start(); //啟動用戶線程
while( getPrimes.isAlive() )
{
System.out.println("Counting the prime number...");//說明主線程在運行
try
{
Thread.sleep(500);//使主線程掛起指定毫秒數,以便用戶線程取得
} //控制權,sleep是static的類方法
catch(InterruptedException e)//sleep方法可能引起的異常,必須加以處理
{
return;
}
}//while循環結束
System.out.println("線程已停止 ,請按回車結束主線程");
try
{
System.in.read();
}
catch(IOException e) {}
} //main方法結束
} //主類結束
class primeThread extends Thread //創建Thread子類,在其run()中實現子線程操作
{
int m_nCircleNum; //循環的上限
primeThread(int Num) { //構造函數
m_nCircleNum = Num;
}
public void run() { //繼承并重載父類Thread的run()方法
int number = 3;
boolean flag = true;
while(true) //無限循環
{
for(int i=2;i<number;i++) //檢查number是否為素數
if( number%i ==0) flag = false;
if(flag)
System.out.println(number+"是素數");
else
System.out.println(number+"不是素數");
number++; //修改number的數值,為下一輪素數檢查做準備
if ( number > m_nCircleNum ) //到達要求檢查數值的上限
{
return; //結束run()方法,結束線程
}
flag = true; //恢復flag,準備檢查下一個numb
try
{ //一輪檢查之后,暫時休眠一段時間
sleep(300);
}
catch(InterruptedException e)
{
return;
}
} //while循環結束
} //run()方法結束
}//primeThread類定義結束
-----------------------------------k2-------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class a extends Applet implements Runnable{
Thread thread1,thread2;
AudioClip m_audio ;
Image[] m_Images= new Image[18] ;
int totalImages = 18; //圖片序列中的圖片總數
int currentImage = 0; //當前時刻應該顯示的圖片序號
boolean t1_stop = false ;
boolean t2_stop = false ;
Button b1 = new Button("stop1");
Button b2 = new Button("stop2");
Panel p1 = new Panel();
public void init() {
setFont(new Font("TimesRoman",Font.PLAIN,20));
setLayout(new BorderLayout());
p1.add(b1); b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t1_stop = true ; m_audio.stop(); } } );
p1.add(b2); b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t2_stop = true ; } } );
add(p1,BorderLayout.SOUTH);
m_audio = getAudioClip(getCodeBase(), "sound/music1.wav");
for(int i=0;i<=totalImages-1;i++)
m_Images[i] = getImage(getDocumentBase(),"images/img"+ i + ".gif");
}
public void start() {
thread1 = new Thread(this,"t1");
thread2 = new Thread(this,"t2");
thread1.start();
thread2.start();
}
public void run() {
String s = Thread.currentThread().getName();
System.out.println("----------"+ s + "----");
if (s.equals("t1")){
while(!t1_stop) {
m_audio.play();
try{
Thread.sleep(300000);
}catch(InterruptedException e){}
}
}
else if (s.equals("t2")){
while(!t2_stop){
repaint();
try{
Thread.sleep(50);
}catch(InterruptedException e){}
}
}
}
public void paint(Graphics g) {
g.drawImage(m_Images[currentImage],75,50,this); //顯示當前序號的圖片
currentImage = ++currentImage % totalImages; //計算下一個應顯示圖片的序號
}
} // end of applet
//---------------------------k3----線程互斥 加鎖---------------
import java.io.* ;
class a {
public static void main(String[] args) {
Sync sync2000 = new Sync() ;
T1 t1 = new T1("t1",sync2000);
T2 t2 = new T2("t2",sync2000);
t1.start();
t2.start();
while(t1.isAlive() || t2.isAlive()) {
if (t1.isAlive()) System.out.println("---------- t1 is Alive");
if (t2.isAlive()) System.out.println("========== t2 is Alive");
try{
Thread.sleep(5000);
}catch(Exception e){}
}
System.out.println(" end of all threads ");
}
}
class T1 extends Thread {
Sync syn ;
T1(String name,Sync s){
super(name);
syn = s ;
}
public void run(){
System.out.println(getName() + " getx()= " + syn.getx());
}
}
class T2 extends Thread {
Sync syn ;
T2(String name,Sync s){
super(name);
syn = s ;
}
public void run(){
// System.out.println(getName() + " getx()= " + syn.getx());
// System.out.println(getName() + " gety()= " + syn.gety());
System.out.println(getName() + " getz()= " + syn.getz());
}
}
class Sync {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in) );
int m=0 ;
public synchronized int getx() {
int x ;
System.out.println(Thread.currentThread().getName() +
" enter getx()-critical");
try{
stdin.readLine();
}catch(Exception e){}
x = m ; x = x + 1 ; m = x ;
return m ;
}
public synchronized int gety() {
int y ;
System.out.println(Thread.currentThread().getName() +
" enter gety()-critical");
try{
stdin.readLine();
}catch(Exception e){}
y = m ; y = y + 1 ; m = y ;
return m ;
}
public int getz() {
int z ;
System.out.println(Thread.currentThread().getName() +
" enter getz()");
synchronized(this){
System.out.println(Thread.currentThread().getName()+
" enter critical");
try{
stdin.readLine();
}catch(Exception e){}
z = m ; z = z + 1 ; m = z ;
return m ;
}
}
}
//--------------------k4 生產者/消費者----------------------
import java.util.*;
class IntQueue extends Vector{
int m_size ;
IntQueue(int capacity){
m_size = capacity ;
}
boolean isFull(){
return size()==m_size ;
}
synchronized void qIn(int x){
while(isFull()){
try{
this.wait();
}catch(InterruptedException e){}
}
add(new Integer(x));
this.notify();
}
synchronized int qOut(){
while(isEmpty()){
try{
this.wait();
}catch(InterruptedException e){}
}
this.notify();
return ((Integer)remove(0)).intValue();
}
}
class Producer extends Thread{
IntQueue pq ;
Producer(IntQueue q){
pq = q ;
}
public void run(){
int i ;
for (int j=1;j<=10;j++){
i = (int)(Math.random()*10);
pq.qIn(i) ;
System.out.println(" produced : " +i );
try{
sleep( (int)(Math.random()*100) );
}catch(InterruptedException e){}
}
}
}
class Consumer extends Thread{
IntQueue cq ;
int fromq ;
Consumer(IntQueue q){
cq = q ;
}
public void run(){
for (int j=1;j<=10;j++){
fromq = cq.qOut() ;
System.out.println(" consumed : " +fromq );
try{
Thread.sleep( (int)(Math.random()*100) );
}catch(InterruptedException e){}
}
}
}
class a {
public static void main(String[] arge){
IntQueue testq = new IntQueue(5);
Producer pro = new Producer(testq);
Consumer con = new Consumer(testq);
con.start();
pro.start();
}
}
//---------------k5 ----------------------------------
import java.io.* ;
class a {
public static void main(String[] args) {
Sync sync2000 = new Sync() ;
T1 t1 = new T1("t1",sync2000);
T2 t2 = new T2("t2",sync2000);
t2.start();
t1.start();
try{
t1.join();
t2.join();
}catch(Exception e){};
System.out.println(" end of all threads ");
}
}
class T1 extends Thread {
Sync syn ;
T1(String name,Sync s){
super(name);
syn = s ;
}
public void run(){
for(int i=1;i<=10;i++){
synchronized(syn){
while(syn.x!=0){
try{syn.wait();}catch(Exception e){}
}
syn.x=i;
syn.notify();
}
}
}
} //end of class T1
class T2 extends Thread {
Sync syn ;
T2(String name,Sync s){
super(name);
syn = s ;
}
public void run(){
while(true){
synchronized(syn){
while(syn.x==0){
try{syn.wait();}catch(Exception e){}
}
System.out.println(syn.x);
if(syn.x==10) return;
syn.x=0;
syn.notify();
}
} //end of loop
}
}
class Sync {
int x=0 ;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -