导航:首页 > 编程语言 > 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多线程笔试编程题相关的资料

热点内容
程序员考核d 浏览:237
自助游中国pdf 浏览:738
安卓p40是什么手机 浏览:79
24cxx编程器 浏览:583
阴阳师如何查看哪个服务器有ID 浏览:308
公务员照片压缩 浏览:450
编译的时候怎么找未定义的函数 浏览:344
有什么我的世界服务器 浏览:298
服务器亮绿灯是什么意思 浏览:629
python画的图如何保存高清版 浏览:491
10的搭接还用加密吗 浏览:365
bytedance这个文件夹是什么意思呢 浏览:588
算法站的客体 浏览:78
src文件夹c语言怎么运行 浏览:25
怎么把已安装的app放到桌面 浏览:948
如何查看苹果手机app是否取消订阅 浏览:774
u盘加密之后手机可以打开吗 浏览:47
单片机串口发射怎么回事 浏览:479
程序员假装自己很忙 浏览:800
程序员能力关键词 浏览:617