java编写两个线程,要求一个输出打印1-52,一个输出打印a-z。打印顺序要12a34b56c......51 52 z
希望大家给力..............那个改过答案的大侠,你自己试过代码吗/我运行不行啊.................
希望大家给力..............
那个改过答案的大侠,你自己试过代码吗/
我运行不行啊.............. 展开
那个改过答案的大侠,你自己试过代码吗/
我运行不行啊.............. 展开
3个回答
展开全部
package semihum.threadTest;
public class Thread1 implements Runnable {
private Object _lock;
public Thread1(Object lock) {
_lock = lock;
}
@Override
public void run() {
try {
synchronized (_lock) {
for (int i = 0; i < 26; i++) {
//System.out.print((char) ('a' + i));
System.out.print((2 * i + 1) + "" + (2 * i + 2));
_lock.notify();
_lock.wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package semihum.threadTest;
public class Thread2 implements Runnable {
private Object _lock;
public Thread2(Object lock) {
_lock = lock;
}
@Override
public void run() {
synchronized (_lock) {
for (int i = 0; i < 26; i++) {
try {
_lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.print((2 * i + 1) + "" + (2 * i + 2));
System.out.print((char) ('a' + i));
_lock.notify();
}
}
}
}
package semihum.threadTest;
public class Main {
public static void main(String[] args){
Object lock=new Object();
Thread t1=new Thread(new Thread1(lock));
Thread t2=new Thread(new Thread2(lock));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//现制,貌似写成a12b34c56...了...您就看着随便改改吧...JDK 1.6
好吧,我改过了,我只想说,LZ你要只是应付作业什么的吧...
public class Thread1 implements Runnable {
private Object _lock;
public Thread1(Object lock) {
_lock = lock;
}
@Override
public void run() {
try {
synchronized (_lock) {
for (int i = 0; i < 26; i++) {
//System.out.print((char) ('a' + i));
System.out.print((2 * i + 1) + "" + (2 * i + 2));
_lock.notify();
_lock.wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package semihum.threadTest;
public class Thread2 implements Runnable {
private Object _lock;
public Thread2(Object lock) {
_lock = lock;
}
@Override
public void run() {
synchronized (_lock) {
for (int i = 0; i < 26; i++) {
try {
_lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.print((2 * i + 1) + "" + (2 * i + 2));
System.out.print((char) ('a' + i));
_lock.notify();
}
}
}
}
package semihum.threadTest;
public class Main {
public static void main(String[] args){
Object lock=new Object();
Thread t1=new Thread(new Thread1(lock));
Thread t2=new Thread(new Thread2(lock));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//现制,貌似写成a12b34c56...了...您就看着随便改改吧...JDK 1.6
好吧,我改过了,我只想说,LZ你要只是应付作业什么的吧...
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我编写了两个类文件,线程ThreadNum用来输出数字,线程ThreadChar用来输出字母,线程都在 testthread包下。
本来我用的方法是Thread.sleep(10),后来想这样只是使当前输出线程停顿了10毫秒,不能很好的体现线程的抢占机制,而且还要处理抛出来的异常,于是我又换成Thread.yield();这样就好多了,有人抢到,当前线程就等着,没人抢到,它就继续执行,于是就能出来两个输出是不确定的交叉输出的效果。
下面是线程ThreadNum;
package testthread;
public class ThreadNum {
public static void main(String[] args){
ThreadChar threadChar = new ThreadChar();
threadChar.start();
for (int i = 1; i <= 52; i++) {
System.out.print(i + " ");
Thread.yield();
}
}
}
下面是线程ThreadChar;
package testthread;
public class ThreadChar extends Thread {
public void run() {
for (int i = 97; i <= 122; i++) {
char c = (char) i;
System.out.print(c + " ");
Thread.yield();
}
}
}
如果嫌上面的代码复杂,简单点就可以写成内部类,下面是代码,存成ThreadNum 文件就可以了:
public class ThreadNum {
public static void main(String[] args){
ThreadChar threadChar = new ThreadChar();
threadChar.start();
for (int i = 1; i <= 52; i++) {
System.out.print(i + " ");
Thread.yield();
}
}
}
class ThreadChar extends Thread {
public void run() {
for (int i = 97; i <= 122; i++) {
char c = (char) i;
System.out.print(c + " ");
Thread.yield();
}
}
}
本来我用的方法是Thread.sleep(10),后来想这样只是使当前输出线程停顿了10毫秒,不能很好的体现线程的抢占机制,而且还要处理抛出来的异常,于是我又换成Thread.yield();这样就好多了,有人抢到,当前线程就等着,没人抢到,它就继续执行,于是就能出来两个输出是不确定的交叉输出的效果。
下面是线程ThreadNum;
package testthread;
public class ThreadNum {
public static void main(String[] args){
ThreadChar threadChar = new ThreadChar();
threadChar.start();
for (int i = 1; i <= 52; i++) {
System.out.print(i + " ");
Thread.yield();
}
}
}
下面是线程ThreadChar;
package testthread;
public class ThreadChar extends Thread {
public void run() {
for (int i = 97; i <= 122; i++) {
char c = (char) i;
System.out.print(c + " ");
Thread.yield();
}
}
}
如果嫌上面的代码复杂,简单点就可以写成内部类,下面是代码,存成ThreadNum 文件就可以了:
public class ThreadNum {
public static void main(String[] args){
ThreadChar threadChar = new ThreadChar();
threadChar.start();
for (int i = 1; i <= 52; i++) {
System.out.print(i + " ");
Thread.yield();
}
}
}
class ThreadChar extends Thread {
public void run() {
for (int i = 97; i <= 122; i++) {
char c = (char) i;
System.out.print(c + " ");
Thread.yield();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询