java線程試題
Ⅰ java線程的題目 求大神解答
1、線程的實現方式有兩種一種是繼承Thread一種是實現Runable。
2、優先順序設置和獲取的示例如下:

線程根據優先順序執行並不根據調用代碼的先後。
Ⅱ java多線程練習題
publicclassTest{
publicstaticObjectobj=newObject();
publicstaticvoidmain(String[]args){
newA().start();
newB().start();
}
}
classAextendsThread{
publicvoidrun(){
try{
synchronized(Test.obj){
for(inti=1;i<31;i+=6){
Test.obj.notify();
System.out.println("線程A:"+i);
System.out.println("線程A:"+(i+1));
System.out.println("線程A:"+(i+2));
Test.obj.wait();
}
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
classBextendsThread{
publicvoidrun(){
try{
synchronized(Test.obj){
for(inti=4;i<31;i+=6){
Test.obj.notify();
System.out.println("線程B:"+i);
System.out.println("線程B:"+(i+1));
System.out.println("線程B:"+(i+2));
Test.obj.wait();
}
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
Ⅲ Java多線程題目
看看是不是你想要的這種方法
public class TheadTest extends Thread{
private static  List<String> list=new ArrayList<String>();
public static void main(String[] args) {
Runnable b = new RunableTest(list);
new Thread(b).start();
Thread a = new TheadTest();
a.start();
}
@Override
public void run() {
add();
}
public void add(){
if(list!=null){
for(int i=1;i<=10;i++){
System.out.println("add:"+i);
synchronized(list) {
list.add(String.valueOf(i));
}
}
}else{
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class RunableTest implements Runnable{
public   List<String> list;
public RunableTest(List<String> list) {
this.list = list;
}
@Override
public void run() {
try {
minus();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void minus() throws InterruptedException {
if(list!=null){
boolean k =true;
while(k){
if(list.size()<=0){
sleep(100);
}else{
for(int i=list.size();i>0;i--){
System.out.println("minus:"+list.get(i-1));
synchronized(list) {
list.remove(i - 1);
}
}
k=false;
}
}
}
}
}
Ⅳ java 線程面試題
我不知道你是不是這個意思,thread1,thread2兩個線程每次讓j增加1,thread3,thread4兩個線程每次讓j減少1,四個線程每個都調用250次相關加減一操作。最終j的結果都是100.
下面程序,總計會列印出1000個數,不管怎麼樣最後一個數永遠是100,即j的終值永遠是100,為了看中間結果運行過程我加了sleep,但無關緊要。你看看符合你要求不?
就像樓上說的,啟動1000條線程不是很明白,不一致的繼續討論
package com.kalort;
public class ThreadTest
{
 public static void main(String[] args)
 {
  Operator operator = new Operator();
  Thread thread1 = new IncreaseThread(operator);
  Thread thread2 = new IncreaseThread(operator);
  Thread thread3 = new DecreaseThread(operator);
  Thread thread4 = new DecreaseThread(operator);
  thread1.start();
  thread2.start();
  thread3.start();
  thread4.start();
 }
}
class IncreaseThread extends Thread
{
 private Operator operator;
 public IncreaseThread(Operator operator)
 {
  this.operator = operator;
 }
 public void run()
 {
  for (int i = 0; i < 250; i++)
  {
   try
   {
    Thread.sleep((long)(Math.random() * 100));
   }
   catch (InterruptedException e)
   {
    e.printStackTrace();
   }
   operator.increase();
  }
 }
}
class DecreaseThread extends Thread
{
 private Operator operator;
 public DecreaseThread(Operator operator)
 {
  this.operator = operator;
 }
 public void run()
 {
  for (int i = 0; i < 250; i++)
  {
   try
   {
    Thread.sleep((long)(Math.random() * 100));
   }
   catch (InterruptedException e)
   {
    e.printStackTrace();
   }
   operator.decrease();
  }
 }
}
class Operator
{
 private int j = 100;
 public synchronized void increase()
 {
  while (100 != j)
  {
   try
   {
    wait(); // 如果另外線程還沒減一就等待
   }
   catch (InterruptedException e)
   {
    e.printStackTrace();
   }
  }
  j++;
  System.out.println(j);
  notify(); // 通知另外線程已經加一了。
 }
 public synchronized void decrease()
 {
  while (100 == j)
  {
   try
   {
    wait();
   }
   catch (InterruptedException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  j--;
  System.out.println(j);
  notify();
 }
}
Ⅳ java題目 編程題目 多線程
public class DoubleThread {
 public static void main(String[] args) {
  Thread t1 = new Thread() {
   @Override
   public void run() {
    for (char i = 'a'; i <= 'z'; i++) {
     System.out.println(i);
    }
   }
  };
  Thread t2 = new Thread() {
   @Override
   public void run() {
    for (char i = 'A'; i <= 'Z'; i++) {
     System.out.println(i);
    }
   }
  };
  t1.start();
  t2.start();
 }
}
Ⅵ 一道java多線程試題
public class NumberPrintDemo {  
    // n為即將列印的數字  
    private static int n = 1;  
    // state=1表示將由線程1列印數字, state=2表示將由線程2列印數字, state=3表示將由線程3列印數字  
    private static int state = 1;  
  
    public static void main(String[] args) {  
        final NumberPrintDemo pn = new NumberPrintDemo();  
        new Thread(new Runnable() {  
            public void run() {  
                // 3個線程列印75個數字, 單個線程每次列印5個連續數字, 因此每個線程只需執行5次列印任務. 3*5*5=75  
                for (int i = 0; i < 5; i++) {  
                    // 3個線程都使用pn對象做鎖, 以保證每個交替期間只有一個線程在列印  
                    synchronized (pn) {  
                        // 如果state!=1, 說明此時尚未輪到線程1列印, 線程1將調用pn的wait()方法, 直到下次被喚醒  
                        while (state != 1)  
                            try {  
                                pn.wait();  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                        // 當state=1時, 輪到線程1列印5次數字  
                        for (int j = 0; j < 5; j++) {  
                            // 列印一次後n自增  
                            System.out.println(Thread.currentThread().getName()  
                                    + ": " + n++);  
                        }  
                        System.out.println();  
                        // 線程1列印完成後, 將state賦值為2, 表示接下來將輪到線程2列印  
                        state = 2;  
                        // notifyAll()方法喚醒在pn上wait的線程2和線程3, 同時線程1將退出同步代碼塊, 釋放pn鎖.   
                        // 因此3個線程將再次競爭pn鎖  
                        // 假如線程1或線程3競爭到資源, 由於state不為1或3, 線程1或線程3將很快再次wait, 釋放出剛到手的pn鎖.   
                        // 只有線程2可以通過state判定, 所以線程2一定是執行下次列印任務的線程.  
                        // 對於線程2來說, 獲得鎖的道路也許是曲折的, 但前途一定是光明的.  
                        pn.notifyAll();  
                    }  
                }  
            }  
        }, "線程1").start();  
  
        new Thread(new Runnable() {  
            public void run() {  
                for (int i = 0; i < 5; i++) {  
                    synchronized (pn) {  
                        while (state != 2)  
                            try {  
                                pn.wait();  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                        for (int j = 0; j < 5; j++) {  
                            System.out.println(Thread.currentThread().getName()  
                                    + ": " + n++);  
                        }  
                        System.out.println();  
                        state = 3;  
                        pn.notifyAll();  
                    }  
                }  
            }  
        }, "線程2").start();  
  
        new Thread(new Runnable() {  
            public void run() {  
                for (int i = 0; i < 5; i++) {  
                    synchronized (pn) {  
                        while (state != 3)  
                            try {  
                                pn.wait();  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                        for (int j = 0; j < 5; j++) {  
                            System.out.println(Thread.currentThread().getName()  
                                    + ": " + n++);  
                        }  
                        System.out.println();  
                        state = 1;  
                        pn.notifyAll();  
                    }  
                }  
            }  
        }, "線程3").start();  
    }  
}
Ⅶ (大一java題目) 多線程
import java.util.*;
public class Exam 
{
	public static void main(String[] args) throws Exception 
	{
		for(Config cg : Exam.getConfigs())
			new MyThread(cg).start();
	}
	private static Config[] getConfigs()
	{
		return new Config[]
		{
			new Config("溫度",20,40,500),
			new Config("濕度",70,90,400),
			new Config("光線強度",20,80,300)
		};
	}
}
class Config
{
	Config(String s,int min,int max,int delay)
	{
		this.s=s;
		this.min=min;
		this.max=max;
		this.delay=delay;
	}
	final String s;
	final int min,max,delay;
}
class MyThread extends Thread
{
	MyThread(Config cg)
	{
		this.cg=cg;
	}
	public void run()
	{
		int temp=cg.max-cg.min+1;
		
		while(!Exit)
		{
			try
			{
				System.out.printf("當前%s:%d",cg.s,R.nextInt(temp)+cg.min);
				System.out.println();
				Thread.sleep(cg.delay);
			}
			catch(Exception ex)
			{
			
			}
		}
	}
	private static final Random R=new Random(100);
	private final Config cg;
	
	static boolean Exit=false;
}
Ⅷ java 線程題目
publicclassTestWaint{
publicstaticvoidmain(String[]args)throwsException{
		finalObjectobj=newObject();
		finalThreadt1=newThread(){
			@Override
			publicvoidrun(){
				synchronized(obj){
					try{
						System.out.println("1");
						obj.wait();
						System.out.println("3");
						obj.notify();
					}catch(InterruptedExceptione){
						e.printStackTrace();
					}
					
				}
			}
		};
		finalThreadt2=newThread(){
			@Override
			publicvoidrun(){
				synchronized(obj){
					try{
							
							System.out.println("2");
							obj.notify();
							obj.wait();
							System.out.println("4");
						}catch(InterruptedExceptione){
							e.printStackTrace();
						}
				}
			}
		};
		t1.start();
		Thread.sleep(100);
		t2.start();
	}
}
Ⅸ 一道java線程題目
沒有同步,m出現了問題。加上synchronized關鍵字
publicsynchronizedvoidrun(){
		for(m=1;m<=1000;m++){
			if(m%2==1){
				System.out.println("奇數:"+m);
			}
			try{
				Thread.sleep((int)(Math.random()*100));
			}catch(InterruptedExceptione){
			}
			if(m%2==0){
				System.out.println("偶數:"+m);
			}
			try{
				Thread.sleep((int)(Math.random()*100));
			}catch(InterruptedExceptione){
			}
		}
	}
Ⅹ 一道java線程練習題
第一步:首先建一個公共對象句柄設置為同步,可以用同步關鍵字控制get和set方法
第二步:在定義一個公共的boolean類型的狀態,可以使用volatile定義這個欄位
第三步:啟動一個線程每隔2秒調用一個上一步中對象句柄的get方法,當發現get不等於NULL的時候,然後更改第二步定義的欄位值改為true,在執行wait()方法,然後在wait()方法後面加上一個輸出消息的語句
第四步:啟動第二個線程創建利用公共對象句柄創建一個對象,循環判斷第二步中的成員是否等於true,如果等於true則調用nodifyAll()在加上break,如果不等於true則調用sleep沉睡5秒左右
完畢!
