| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1008 人关注过本帖
标题:新手初学多线程,对多线程理解不够好.刚写一个小程序,高手看看错误在哪里
只看楼主 加入收藏
hys1986
Rank: 2
等 级:论坛游民
帖 子:9
专家分:21
注 册:2011-5-22
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:7 
新手初学多线程,对多线程理解不够好.刚写一个小程序,高手看看错误在哪里
题目要求: 5个读线程,2个写线程,要求从5个读线程读入字符串,转化成大写后,放入写线程
小弟的感觉.重点.就是如果让读线程不能和两个写线程冲突问题.我的想法是写了两个写线程的CLASS,必须JOIN后才能继续(也就是不冲突),在读线程里通过取一个随机数,决定放入第一个写线程还是第二个写线程

小弟是以C++入门的,对JAVA完全有点晕,本身就不会多线程,现在用JAVA写就更糊涂了.求改错.先谢谢了...
程序代码:
public class ReadThread implements Runnable//读线程
   {
    private string name;
   

        public ReadThread(string name1)
           {
        this.name=name1;   

        }
   

    public void run()
    {
        int x=this.name.length();
        String m="";
        for(int i=0;i<x;i++)
         {
             if(this.name.charAt(i)>='a' && this.name.charAt(i)<='z')
               m=m+(char)(this.name.charAt(i)-32);
              else
             m=m+(char)(this.name.charAt(i));   

        }
        boolean b1=nextDBoolean();//这里产生一个随机布尔,来判定是用第一个写线程或者第二个写线程
        if (b1=true)
        {
            WriteThread1 wt1= new WriteThread(m);
            Thread t01= new Thread(wt1);
            t01.start();
            t01.join();
           

        }
        else

        {
            WriteThread2 wt2= new WriteThread(m);
            Thread t02= new Thread(wt2);
            t02.start();
            t02.join();
        }
    }
   

   

}

public class WriteThread1 implements Runnable//第一个写线程
   {
    private string name;
   

        public WriteThread1(string name1)
           {
        this.name=name1;   

        }
   

    public void run()
    {
          //code to send this.name somewhere else
    }  

}

public class WriteThread2 implements Runnable//第二个写线程,一模一样
   {
    private string name;
   

        public WriteThread2(string name1)
           {
        this.name=name1;   

        }
   

    public void run()
    {
          //code to send this.name somewhere else
    } 

}


public class TestRunnable//主程式
    {  

       

        public static void main(String[] args)
        {  

            //initialize 5 thread, for example
            ReadThread rt1= new ReadThread("abcde");
            ReadThread rt2 new ReadThread("defgh");
            ReadThread rt3 new ReadThread("a34cd");
            ReadThread rt4= new ReadThread("ABcDE");
            ReadThread rt5= new ReadThread("lzxC8");
           

            Thread t1= new Thread(rt1);
            Thread t2= new Thread(rt2);
            Thread t3= new Thread(rt3);
            Thread t4= new Thread(rt4);
            Thread t5= new Thread(rt5);
           

            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();   

           

        }
    }



[ 本帖最后由 hys1986 于 2011-5-23 07:53 编辑 ]
搜索更多相关主题的帖子: private 多线程 字符串 JAVA 
2011-05-23 07:50
w123012306
Rank: 9Rank: 9Rank: 9
来 自:湖南
等 级:蜘蛛侠
威 望:4
帖 子:307
专家分:1180
注 册:2010-4-22
收藏
得分:5 
错误不一般的多;大部分是打错代码!

楼上,楼下的一定要幸福开心哦!
2011-05-24 08:37
w123012306
Rank: 9Rank: 9Rank: 9
来 自:湖南
等 级:蜘蛛侠
威 望:4
帖 子:307
专家分:1180
注 册:2010-4-22
收藏
得分:0 
楼主代码中,1.是String 不是string;
            2.主方法中  “ReadThread rt2 new ReadThread("defgh");
                          ReadThread rt3 new ReadThread("a34cd");”  “=”号呢?
            3. 还有 “boolean b1=nextDBoolean();//这里产生一个随机布尔,来判定是用第一个写线程或者第二个写线程“
            4.方法是nextBoolean(); 应该是boolean b1=Random.nextBoolean();但这里不能用这方法,全局是静态,此方法是非静态!
            5.如果这些代码在一个java文件下的话,只能有一个public class ***{}  ***为java文件名;
            6.join();方法抛出了异常,应该用 try{}catch进行处理;
            7.WriteThread1 wt1= new WriteThread(m);WriteThread2 wt2= new WriteThread(m);这里,如果楼主是向上转型的话,不能这样转; 如果不是转型,则实例化前后一样为WriteThread1 wt1= new WriteThread1(m);WriteThread2 wt2= new WriteThread2(m);

楼上,楼下的一定要幸福开心哦!
2011-05-24 08:48
w123012306
Rank: 9Rank: 9Rank: 9
来 自:湖南
等 级:蜘蛛侠
威 望:4
帖 子:307
专家分:1180
注 册:2010-4-22
收藏
得分:0 
import java.util.*;
 class ReadThread implements Runnable//读线程
    {
     private String name;
   
 
        public ReadThread(String name1)
            {
         this.name=name1;   
 
        }
        public boolean nextBoolean() {
           double i=Math.floor(Math.random()*10);
            if(i%2==0){
               return true;
            }
            else{
              return false;
            }
         }
 
    public void run()
     {
         int x=this.name.length();
         String m="";
         for(int i=0;i<x;i++)
          {
              if(this.name.charAt(i)>='a' && this.name.charAt(i)<='z')
                m=m+(char)(this.name.charAt(i)-32);
               else
              m=m+(char)(this.name.charAt(i));   
 
        }
         //boolean b1=Random.nextBoolean();//这里产生一个随机布尔,来判定是用第一个写线程或者第二个写线程
         
         if (this.nextBoolean())
         {
             WriteThread1 wt1= new WriteThread1(m);
             Thread t01= new Thread(wt1);
             t01.start();
             try{
             t01.join();
            }catch(Exception e){
               e.printStackTrace();
            }
 
        }
         else
 
        {
             WriteThread2 wt2= new WriteThread2(m);
             Thread t02= new Thread(wt2);
             t02.start();
              try{
             t02.join();
            }catch(Exception e){
               e.printStackTrace();
            }
 
         }
     }
   
 
   
 
}
 
 class WriteThread1 implements Runnable//第一个写线程
    {
     private String name;
   
 
        public WriteThread1(String name1)
            {
         this.name=name1;   
 
        }
   
 
    public void run()
     {
           //code to send this.name somewhere else
     }  
 
}
 
class WriteThread2 implements Runnable//第二个写线程,一模一样
    {
     private String name;
   
 
        public WriteThread2(String name1)
            {
         this.name=name1;   
 
        }
   
 
    public void run()
     {
           //code to send this.name somewhere else
     }

}
 

public class TestRunnable//主程式
     {  
 
      

        public static void main(String[] args)
         {  
 
            //initialize 5 thread, for example
             ReadThread rt1= new ReadThread("abcde");
             ReadThread rt2 =new ReadThread("defgh");
             ReadThread rt3 =new ReadThread("a34cd");
             ReadThread rt4= new ReadThread("ABcDE");
             ReadThread rt5= new ReadThread("lzxC8");
            
 
            Thread t1= new Thread(rt1);
             Thread t2= new Thread(rt2);
             Thread t3= new Thread(rt3);
             Thread t4= new Thread(rt4);
             Thread t5= new Thread(rt5);
            
 
            t1.start();
             t2.start();
             t3.start();
             t4.start();
             t5.start();   
 
           
 
        }
     }
不知道楼主,要实现什么,我只改了错!

楼上,楼下的一定要幸福开心哦!
2011-05-24 08:51
buffer
Rank: 5Rank: 5
等 级:职业侠客
帖 子:73
专家分:326
注 册:2010-12-31
收藏
得分:5 
程序代码:
for(int i=0;i<x;i++)
          {
              if(this.name.charAt(i)>='a' && this.name.charAt(i)<='z')
                m=m+(char)(this.name.charAt(i)-32);
               else
              m=m+(char)(this.name.charAt(i));   

        }

其实字符串转大写不用这么麻烦的,直接调用String类的toUpperCase()方法就行了。
另外,
5个读线程,2个写线程,要求从5个读线程读入字符串,转化成大写后,放入写线程

什么是读线程?什么是写线程?读入字符串,从哪儿读?放入写线程,何为放入?
把题目描述清楚,别人才可能给你帮助。
2011-05-24 14:04
hys1986
Rank: 2
等 级:论坛游民
帖 子:9
专家分:21
注 册:2011-5-22
收藏
得分:0 
谢谢楼上两位的回复.感谢2,3L改错
这个是一道考试题.
我感觉其实是考验的是如何来分配5个读和2个写的线程,不是要真正实现什么...所谓写线程就是直接把5个字符丢过去就行了..原题如此

原题如下..

 Implement a multithreaded application in the following manner: 5 reader threads read from 5 character data streams convert data to upper case and pass the converted data to 2 writer threads. It does not matter what language or threading package you use.


[ 本帖最后由 hys1986 于 2011-5-24 20:22 编辑 ]
2011-05-24 20:06
buffer
Rank: 5Rank: 5
等 级:职业侠客
帖 子:73
专家分:326
注 册:2010-12-31
收藏
得分:0 
回复 6楼 hys1986
既然是多线程,那么你随机找一个WriterThread就不合适了。你可以在reader和writer之间设置一个缓冲区,reader和writer之间通过这个buffer交换数据,只要为这个buffer加上同步就可以了。
2011-05-24 22:44
hys1986
Rank: 2
等 级:论坛游民
帖 子:9
专家分:21
注 册:2011-5-22
收藏
得分:0 
SOGA 那这样看行不行
我新建了一个STRING CLASS, 里面包含了字符串X和布尔READY

首先
主程序里创建两个新的STRING CLASS, A, B
创建5个读进程..读进程读到东西之后,就把STRING CLASS里的字符串设置到STRING CLASS的X里, READY设为TRUE,但是这样只能把读线程1-3给A,4-5给B(这里其实也可以随机分配)
创建两个写进程,分别不断读取A,B如果A,B的READY为TRUE,则发送文件,然后将READY改为FALSE
给A B加同步锁
这样可行么..


class string//the string class, to indicate that weather the string needs to be send and it's content
{
   private String x;
   private boolean ready=false;//indicator, if it is true, then the string needs to be send out
  public string(){
       this.x="";
       this.ready=false;
   }//default constructor
   public string(String name){
       this.x=name;
       this.ready=true;
   }//costructor with content
   public void SetString(String name){
       this.x=name;
   }//set the string
   public void SetReady(boolean ready){
       this.ready=ready;
   }//set the indicator
   public String getX(){
       return x;
   }//get the string
   public boolean getReady(){
       return ready;
   }//get the indicator   
}


此外还有个问题..主程序创建了一个CLASS的话,
我能够在进程的程序里里直接用这个CLASS的东西,而不必要将这个CLASS传递给这个这个进程么?

比如 我主程序这么开头
         string string1=new string();
         string string2=new string();
在其他THREAD里能直接调用STRING1, STRING2么.还是必须要在创建THREAD的时候就把STRING1 PASS过去..



[ 本帖最后由 hys1986 于 2011-5-25 00:22 编辑 ]
2011-05-24 23:58
快速回复:新手初学多线程,对多线程理解不够好.刚写一个小程序,高手看看错误在哪 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012498 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved