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秒左右
完毕!