الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Thread

مغلق
بدأه المبرمج1 في 6 أبريل 2004 · 8 رد · 838 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

the following code is vary simple Thread code..

it simply print a string Character by character ... and it sleep between each printing ..

so you will se it printing one by one ..

so simple ... just to clear the idea of Thread

any comment .. just inform me

public class MyThread  extends Thread{

  private static int ThreadNumber=1;
  private int SleepTime;
  // construct
  public MyThread()
  {
  super("Thread Number: "+ThreadNumber);
  ThreadNumber++;
  }// end constructor


  // the defult start method----------------------------------------------------
   public void run()
   {
   }// end run method

 // to print character by catacter
 public void printThread(String text)
 {
   System.out.print("\n");
   for (int i = 0; i < text.length(); i++)
     {
       try{
           Thread.sleep(300);//<- for how long the Thread will sleep
           }catch(InterruptedException exception)
           {exception.printStackTrace();}
       // print
       System.out.print(text.charAt(i));

     }//end for
 }//end method printThread


// the main method--------------------------------------------------------------
 public static void main(String[] args) {

    System.out.println("\n\n\tStart the main Method:\n");


    MyThread t1 = new MyThread();
    t1.printThread("The Thread is Printing the String Character by character ... "); //<- enter any string

    System.out.print("\n End the Main...");


  }//end the main method


}//end class MyThread
#2

Nice Idea !!!

But you are not using threads. you are calling a function "printThread" on onther class. That could have been done without extending the "Thread" Class. One more thing, to fork a new thread you have to write this code

ThreadObject.start();

which you didn't do...

اقتباس
// the main method--------------------------------------------------------------
public static void main(String[] args) {

   System.out.println("\n\n\tStart the main Method:\n");


   MyThread t1 = new MyThread();
   t1.printThread("The Thread is Printing the String Character by character ... "); //<- enter any string
!@#!@#!@##@!@#!@@@#@!@ BUG BUG BUG "Where is t1.start();!!!!!!!!!!!!
   System.out.print("\n End the Main...");


 }//end the main method

I'll try to write something that uses threads.

I'll do that later. I have classes tommorow so I gotta hit the sack. :))

تم تعديل هذه المشاركة بواسطة code2263 في 7 أبريل 2004 في 12:09

#3

هذا كود يستخدم Threads

وسوري على التأخير

/*
 * Count.java
 *
 * Created on April 10, 2004, 12:21 AM
 */


/**
 *
 * @author  code2263
 */
public class Count {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Counter counter1=new Counter("counter1");
        counter1.start();
        Counter counter2=new Counter("counter2");
        counter2.start();
        
        for(int i=1; i<=20;i++){
                System.out.println("Main thread is counting "+i);
                try{
                    Thread.sleep(40);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
    }
    
    public static class Counter extends Thread{
        public Counter(String name){
            super(name);
        }
        public void run(){
            System.out.println("Thread "+this.getName()+" will start counting.");
            for(int i=1; i<=10;i++){
                System.out.println("Thread "+this.getName()+" is counting "+i);
                try{
                    Thread.sleep(50);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            System.out.println("Thread "+this.getName()+" finished counting.");
        }
    }
    
}
#4

OK ... thanks

but i was thinking ..

if i used this way ... then each thread class can do only one function ...

is there a way to add more than one function to the thread ...

so diffrent threads from the same class can do diffrent functions ..

#5

أشكركم أخوانى على هذه الأمثله للـThreads

فى البرنامج الأول هل يمكن أن نضيف عبارة جديده تعرض مع العباره الأولى في نفس الوقت

حيث أنى أضفتها ولكنها تعرض بعد إنتهاء العباره الألى

ةماهى الطريقة المستخدمه

وشكراً

#6

أسف على التأخير....

المبرمج1:

you can created any number of classes that extend Thread class. and so you can have many threads that do different things.

Does that answers you question?. if not plz let me know

السكب:

الـThreads لا تظمن لك أي ترتيب من ناحيد أي Thread سيبدأ أو ينتهي قبل الأخر. إذا كنت تريد أن تجعل Thread تتفاهم مع بعضها البعض فهذا ما يعرف باSynchronization أو التزامن...حاول أن تبحث من مصدر لشرح هذه المشكله... موقع صن جافا يحتوي على هذه الدروس...أعتذر عن تقديم أي شيء في الوقت الحالي لأن الأمتحانات النهائيه على الأبواب...

أبشروا بكل خير إذا رجعت

#7

I meant that when you run the above program you will see a string moving

my question is can creat another string that is moving with rhe prevous string at a same time?

#8

Mr. code2263

thank you for your replay ... i think get the idea

thanks again

#9

first ... i wont to thank Mr.code2263

...

then to السكب

see the code below ... you can add as much thread as you like ... and you can assign each one his own string ...

i hope that what you ask for..

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class MyThread  extends Thread{

  private String string;
  private static int ThreadNumber = 1;
  public MyThread()
  {
    this("");
  }

  public MyThread(String stringTXT)
  {
    super("ThreadNumber_"+ThreadNumber);
    ThreadNumber++;
    string = new String(stringTXT);
  }

  //set the strign that will be printed-----------------------------------------
  private void setString(String stringTXT)
  {
  string = new String(stringTXT);
  }// end method setString

  //  get the string that will be printed---------------------------------------
  private String getString()
  {return string;}// end method getString

  // run method-----------------------------------------------------------------
  public void run()
  {
    for(int i = 0; i < string.length(); i ++)
    {
      System.out.print(string.charAt(i));
      try{
            Thread.sleep(100);
            }catch(InterruptedException exception){exception.printStackTrace();}
    }
  }//end run method

  // the main method------------------------------------------------------------
  public static void main(String[] args) {

    MyThread t1 = new MyThread("1111111111111"); // <- 1st string

    MyThread t2 = new MyThread("0000000000000");//<- 2nd string

    t1.start();
    t2.start();


  }//end the main method


}// end class MyThread2

if somthing is not clear ... just inform me

هذا الموضوع مغلق.

مواضيع مشابهة