導航:首頁 > 編程語言 > java多線程筆試編程題

java多線程筆試編程題

發布時間:2022-06-03 21:32:06

java多線程練習題

public class Thread11 {
public static void main(String[] args) {
GThread g = new GThread("烏龜");
ZThread z = new ZThread("兔子");
g.start();
z.start();
}
}

class GThread extends Thread{//烏龜
private String name;
public GThread(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 1000; ) {
try {
sleep(100);//1000
i++;
System.out.println(name + "跑了" + i + "米了");
if(i%100==0){
sleep(500);
System.out.println(name + "休息了500ms");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}
class ZThread extends Thread{//兔子
private String name;
public ZThread(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 1000; ) {
try {
sleep(100);//1000
i+=5;
System.out.println(name + "跑了" + i + "米了");
if(i%20==0){
sleep(500);
System.out.println(name + "休息了500ms");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

㈡ 【在線】高分求助JAVA多線程編程題 (極為簡單的多線程顯示字元)

你確定B是3毫秒?

class Test
{
public static void main (String[] args) {
new A('A',500).start();
new A('B',3).start();
}
}
class A extends Thread
{
char c;
int i;
A(char c,int i){
this.c=c;
this.i=i;
}
public void run()
{
for(;;)
{
try{
sleep(i);
}
catch(Exception e){}
System.out.println (c);
}
}
}

㈢ 用JAVA寫一個多線程程序,如寫四個線程,二個對一個變數減一,輸出。

public class ThreadTest1{
private int j;
public static void main(String args[]){
ThreadTest1 tt=new ThreadTest1();
Inc inc=tt.new Inc();
Dec dec=tt.new Dec();
for(int i=0;i<2;i++){
Thread t=new Thread(inc);
t.start();
t=new Thread(dec);
t.start();
}
}
private synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
}
private synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
}
class Inc implements Runnable{
public void run(){
for(int i=0;i<100;i++){
inc();
}
}
}
class Dec implements Runnable{
public void run(){
for(int i=0;i<100;i++){
dec();
}
}
}
}

㈣ 遇到一個筆試題,關於java 多線程的,請牛人多指點,小弟菜鳥一個~~

我的做法就是,把int改為Integer,然後每個方法加上synchronized,但是,你說的線程,是怎麼回事?把C繼承Thread,是Runnable,實現run()方法?

㈤ JAVA多線程 編程題兩個案例,不會寫,求大神寫出代碼,萬分感謝,多線程還沒學。

/*
class A extends Thread
{
public void run()
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
System.out.println("A ERROR!");
}
System.out.println("AAAA");
}
}

class B extends Thread
{
public void run()
{
try
{
Thread.sleep(2000);
}catch(Exception e)
{
System.out.println("B ERROR!");
}
System.out.println("BBBB");
}
}

class C extends Thread
{
public void run()
{
try
{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println("C ERROR!");
}
System.out.println("CCCC");
}
}

public class Test_1
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();

a.start();
b.start();
c.start();
}
}*/

public class Test_1
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();
Thread ta = new Thread(a);
Thread tb = new Thread(b);
Thread tc = new Thread(c);

ta.start();
tb.start();
tc.start();
}
}

class A implements Runnable
{
public void run()
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
System.out.println("A ERROR!");
}
System.out.println("AAAA");
}
}

class B implements Runnable
{
public void run()
{
try
{
Thread.sleep(2000);
}catch(Exception e)
{
System.out.println("B ERROR!");
}
System.out.println("BBBB");
}
}

class C implements Runnable
{
public void run()
{
try
{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println("C ERROR!");
}
System.out.println("CCCC");
}
}
案例一的兩種方法已經寫好;現在有事,晚上再把案例二代碼寫一下,應該沒關系吧!
抱歉,是一個線程類,我看錯了,晚上重發一下代碼!

㈥ 一道java多線程試題

B, 答案錯了,哪個老師出的題?BS TA。
兩種方式,實現介面為Rnnable,繼承父類為Thread

閱讀全文

與java多線程筆試編程題相關的資料

熱點內容
腰椎壓縮性骨折症狀 瀏覽:466
絕地求生全軍出擊的伺服器什麼時候開 瀏覽:490
怎麼入侵游戲伺服器篡改數據 瀏覽:734
線性壓縮機原理 瀏覽:540
javanewinstance 瀏覽:815
程序員第一玄學 瀏覽:496
精品源碼怎麼算 瀏覽:490
加密技術在現實中應用體會 瀏覽:180
單片機如何換晶振 瀏覽:206
合並兩個數組java 瀏覽:13
命令標注圓半徑怎麼用 瀏覽:659
出差人員電腦加密 瀏覽:248
文件夾0項什麼意思 瀏覽:603
空調冷媒壓縮機啟動 瀏覽:912
流氓文件夾怎麼強制刪除 瀏覽:783
嵌入式基礎命令 瀏覽:526
當程序員必學的編程語言 瀏覽:907
pro文件夾有什麼用 瀏覽:357
我的世界伺服器有人開掛如何踢出 瀏覽:482
程序員有天賦 瀏覽:640