java两个线程交替打印
Ⅰ 本人是java编程菜鸟:关于多线程的一个问题,希望高人给予帮助;就是连个线程,交替打印两个线程的内容!
Thread1 th1 = new Thread_Test().new Thread1();
Thread2 th2 = new Thread_Test().new Thread2();
这两句你是创建了两个不同的Thread_Test对象,他们都有各自的obj。这等于th1和th2在不同对象上工作,你的锁就没有意义了,th1执行了一次后锁住,th2开始就直接锁住。
记住同步永远是多个线程在一个对象上作用才讨论的问题,这个你肯定也明白,就是对对象创建机制有些不清楚。
Thread1 th1 = new Thread_Test().new Thread1();
Thread2 th2 = new Thread_Test().new Thread2();
th1.start();
th2.start();
这段要改成
Thread_Test tt = new Thread_Test();
Thread1 th1 = tt.new Thread1();
Thread2 th2 = tt.new Thread2();
th1.start();
th2.start();
一样可以的
Ⅱ 谁帮我实现以下Java中的线程交替执行控制,要求按顺序输出1到100,代码如下。
稍微修改下 未测试
packagecom.leejiliang.HomeworkDemo2;
/**
*定义打印输出类,定义两个数据输出方法,分别用于输出偶数和奇数
*
*@authorAdministratoreven偶数uneven奇数
*/
classPrintNumber{
booleanisEven=true;
publicvoidprintEven(){
for(inti=0;i<=100;i=i+2){
if(i%2==0){
synchronized(this){
while(!isEven){
try{
this.wait();
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
System.out.println("偶数:"+i);
isEven=false;
this.notify();
}
}
}
}
//输出1到100之间的奇数
publicvoidprintUnEven(){
for(inti=1;i<=99;i=i+1){
if(i%2!=0){
synchronized(this){
while(isEven){
try{
this.wait();
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
System.out.println("奇数:"+i);
isEven=true;
this.notify();
}
}
}
}
}
/**
*
*@authorAdministrator定义输出偶数的线程类
*/
{
privatePrintNumberpn=null;
publicThreadForEven(PrintNumberpn){
this.pn=pn;
}
@Override
publicvoidrun(){
pn.printEven();
}
}
/**
*定义输出奇数的线程类
*
*@authorAdministrator
*
*/
{
privatePrintNumberpn=null;
publicThreadForUneven(PrintNumberpn){
this.pn=pn;
}
@Override
publicvoidrun(){
pn.printUnEven();
}
}
//主函数,执行两个线程
classZeroToOneHundred{
publicstaticvoidmain(String[]args){
PrintNumberpn=newPrintNumber();
ThreadForEvente=newThreadForEven(pn);
ThreadForUneventu=newThreadForUneven(pn);
Threadthreadeven=newThread(te);
Threadthreaneven=newThread(tu);
threadeven.start();
threaneven.start();
}
}
Ⅲ Java程序如何让两张表交替打印
for(i=0;i<10;i++){
a[i]
b[i];
}
这样的思路不就行了么……
难道你是声明一个sheet然后把a操作完了再指向b么……
Ⅳ java多线程问题。两个线程交替打印。例如第一个线程打印1,接着第二个线程打印100,接着第一个线程打印2。
你这样写两个线程实例t1和t2间没有交互通信,各跑各的,当然不会达到你上面说的那个结果。要想达到你上面说的那个效果,必须进行线程间通信。比如,你可以让两个线程实例都对方的引用,在run函数里执行打印的方法后,就让t2跑,t1去睡觉(sleep())。等t2打印完后,又让t1跑,让t2去睡觉,这样即可。 给你思路,代码我就不写了。
Ⅳ 关于java 两个线程轮换打印1-10的问题
先回答问题:
1、为什么打印到10就不打印了?因为打印到10 以后,满足1%10 == 0 ,然后线程wait()了,却没有任何其他的线程唤醒,当然不打印了!没有线程唤醒?是的!下面的pn.notify()是无法唤醒的,因为没有执行的机会。另外一个线程可能会执行这句话,但是那个pn和这个pn不是同一个对象,所以无法唤醒!若要其他线程唤醒此线程,则需要使用同一个对象同一把锁进行同步!
2、0和1交替打印很正常啊,你多执行几次,会发现每次和每次的打印都不一样,都是随机打印。为什么?你的同步使用是错误的。
结论:线程同步可以使用synchronized关键字,而该关键字要求若多个线程之间进行同步,需要使用恰当的对象锁,否则结果不可预料。
Ⅵ Java三个线程交替打印abc
一定要规定完全有序/有规律的话,不要用线程。线程本来就是不确定哪个先执行哪个后执行的
Ⅶ JAVA双线程 用两个线程交替打印从1到100。 如:线程1 1 线程2 2
publicclassSpider{
classEggimplementsRunnable{
Stringname;
publicEgg(Stringname){
this.name=name;
}
Threadthread;
publicvoidstart(){
if(null==thread){
thread=newThread(this);
thread.setName(name);
thread.start();
}
}
publicsynchronizedvoidstop(){
if(null!=thread){
thread.interrupt();
thread=null;
notifyAll();
}
}
publicvoidrun(){
Threadme=Thread.currentThread();
while(me==thread){
if(count>99){
stop();
break;
}
try{
Thread.sleep(50);
}catch(Exceptione){}
System.out.println(me.getName()+""+count++);
}
}
}
intcount=1;
publicSpider(intnum){
for(inti=0;i<num;i++){
Eggegg=newEgg("线程"+(i+1));
egg.start();
}
}
publicstaticvoidmain(String[]args){
newSpider(2);
}
}
Ⅷ java 多线程编程 交替打印大小写字母的问题
这个就是多线程的一个考察:
publicclassRunTest{
publicstaticvoidmain(String[]args){
Myprintmp=newMyprint();
newCapital(mp).start();
newLowercase(mp).start();
}
}
classMyprint{
booleanflag=true;
inti=0;
intj=0;
publicvoidDa(){
while(i<26){
if(flag){
System.out.print((char)('A'+i));
i++;
this.flag=false;
}
}
}
publicvoidXiao(){
while(j<26){
if(!flag){
System.out.print((char)('a'+j));
j++;
this.flag=true;
}
}
}
}
classCapitalextendsThread{
Myprintmy=null;
publicCapital(Myprintmy){
this.my=my;
}
@Override
publicvoidrun(){
my.Da();//打印大写
}
}
classLowercaseextendsThread{
Myprintmy=null;
publicLowercase(Myprintmy){
this.my=my;
}
@Override
publicvoidrun(){
my.Xiao();//打印小写
}
}
Ⅸ java实现两个线程,一个打印奇数,一个打印偶数,这两个线程如何并发执行
package test;
public class Test33{
private static int state = 1;
private static int num1 = 1;
private static int num2 = 2;
public static void main(String[] args) {
final Test33 t = new Test33();
new Thread(new Runnable() {
@Override
public void run() {
while(num1<100){
//两个线程都用t对象作为锁,保证每个交替期间只有一个线程在打印
synchronized (t) {
// 如果state!=1, 说明此时尚未轮到线程1打印, 线程1将调用t的wait()方法, 直到下次被唤醒
if(state!=1){
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 当state=1时, 轮到线程1打印5次数字
for(int j=0; j<1; j++){
System.out.println(num1);
num1 += 2;
}
// 线程1打印完成后, 将state赋值为2, 表示接下来将轮到线程2打印
state = 2;
// notifyAll()方法唤醒在t上wait的线程2, 同时线程1将退出同步代码块, 释放t锁
t.notifyAll();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while(num2<100){
synchronized (t) {
if(state!=2){
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j=0; j<1; j++){
System.out.println(num2);
num2 += 2;
}
state = 1;
t.notifyAll();
}
}
}
}).start();
}
}
Ⅹ 1、多线程编程,两个线程,一个负责打印大写字母,一个负责打印小写字母,实现大小写字母交替打印。
线程1打印大写字母:
void t1()
{
for(char c = 'A';c<='Z';c++)
{
while(!MyPrintf(true, c)){延迟n毫秒;}
}
}
线程2打印小写字母:
void t2()
{
for(char c='a';c<='z';c++)
{
while(!MyPrintf(false, c)){延迟n毫秒;}
}
}
bool isCapital;
synchronised bool MyPrintf(bool isCap, char c){
if(isCap != isCapital)
return false;
//打印c
isCapital = !isCapital;
return true;
}
大概思路是以上这样。