Ⅰ 本人是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;
}
大概思路是以上這樣。