MohamedNagyMostafa/GThread-JavaLibrary

Advanced version of java threads which provides programmers a full control on their threads in a little code

★ 1Forks 2JavaGitHub ↗Compare
codingjavajava-libraryprogrammer-toolprogrammersprogramming-languagesthreadthreadingthreads

README

GThread Library

Brief

A Background threading system that works with efficient manner. Java programmers have a full control on their threads in a few lines of code while saving memory.

Features

  • You can access the thread's output when the thread's progress is finished.
  • GThread have containers which run threads in scheduled manner.
  • Containers have ability to specify limit number of running threads and the same time.

How it works

  • GThread

    • You specify the type of object which you want to return when GThread process is over. GThread is an abstract class with two abstract methods:
    • onProgress(): in which you write your code that you want to execute when GThread starts running.
    • onFinished(Object): This method is called when GThread progress is finished. The incoming object is that the object which is going to update during GThread progress. The type of this object is generic.



    • You can start GThread by calling start() method.

    Methods

    • start (): To start GThread progress.
    • onProgress (): Called when GThread starts.
    • onFinished (): Called when GThread progress ends.
    • gthreadState(): Returns state of gthread.
      • GThread.G_THREAD_IDLE: When gthread is created but isn't started yet.
      • GThread.G_THREAD_RUNNING: When gthread is running.
      • GThread.G_THREAD_TERMINATED: When gthread work is completed.
  • Containers

    • SheduleGThread

      • Container which takes GThreads and the limits the GThreads running at the same time to a specific number. SheduleGThread take the number of threads you want to execute in the same time and array of GThread.
      • ScheduleGThread is an abstract class also with onScheduleFinished() method which is called when ScheduleGThread finishs all GThreads progresses. You can start SheduleGThread by calling start () method.
      • ScheduleGThread first parameter is the limit number (e.g. 2 in the following image as a number of GThreads which are going to run together at the same time).
        Note: ScheduleGthread has verargs argument so you can set GThreads as an array or individualy.



      • To demystify the first parameter of ScheduleGThread let's take an example about how it really work.
      • Let’s assume that you have 10 GThreads and you want to run them two by two in order.
      • Initial state of Schedule when you create instance and you don't launch it yet. (Waiting: GThreads are hold but in idle state | In Working Stage: Current GThreads which are running| Finished: GThreads which finished their progress)





      • Now you called start () method for scheduleGThread ... it's going to run first two GThreads only because you set ScheduleGThread limit to 2.



      • As we can see the first two GThreads only are running and the others in waiting state.



      • Okey, Now assume that one of GThread is finished what will happen? The next GThread is going to start... Yes it's amazing, right? :D



      • Then this process will repeat until all GThreads in waiting state are finished. After that the onScheduleFinished() method will be called.



      • Methods

        • start(): Returns ScheduleGThreadLinked.SCHEDULE_LINK_ACCEPT_RESPONSE if ScheduleGThread is started successfully otherwise returns ScheduleGThreadLinked.SCHEDULE_LINK_REJECT_RESPONSE. (We are going to talk about this point in details later)
    • ScheduleGThreadLinked

      • It's similer to SheduleGThread, but more dynamic. In ScheduleGThread you initialize the instance with array of GThreads and the limit of the of GThreads which are going to run at the same time. ScheduleGThreadLinked takes the same parameters and it functions same as ScheduleGThread but you can add GThread using add (Gthread gthread) method and remove GThread using remove(GThread gthread) method during ScheduleGThreadLinked running state.



      • To be more clear let's assume that we have 3 GThread we want to run, the first two are passed to ScheduleGThreadLinked and we set limitation of GThread running to 2, then we start the process by start() method then we add the next thread. So in initial state of ScheduleGThreadLinked is going to be something like that



      • Then ScheduleGThread is going to start both gthread1 and gthread2 because its limitation is 2 as we said before.



      • Okay, Assume gthread1 is finished and the gthread2 is still running then what will happen when we add gthread3.. ? Yes, as you thought. gthread3 is going to run automatically because your limit is set to 2 and gthread2 is the only GThread running now.



      • - Now you can think that ScheduleGThreadLinked takes a lot of memory whenever GThread which are contained by ScheduleGThreadLinked are finished. Because it's waiting another GThread!! I want to reassure you because it's just going to start when it has GThreads need to execute otherwise it is going to stop completely and when you add new gthread then it create itself automatically and start again.
      • Methods

        • start(): Starts the ScheduleGThreadLinked. Returns ScheduleGThreadLinked.SCHEDULE_LINK_ACCEPT_RESPONSE if ScheduleGThreadLinked started successfully otherwise returns ScheduleGThreadLinked.SCHEDULE_LINK_REJECT_RESPONSE. (We are going to talk about this point in details later).
        • add(GThread gthread): To add new GThread to ScheduleGThreadLinked. Returns ScheduleGThreadLinked.SCHEDULE_LINK_ACCEPT_RESPONSE when adding process is succeeded otherwise returns ScheduleGThreadLinked.SCHEDULE_LINK_REJECT_RESPONSE. (We are going to talk about this point in details later).
        • remove(GThread gthread): To remove existing GThread. Returns ScheduleGThreadLinked.SCHEDULE_LINK_ACCEPT_RESPONSE when removing process is succeeded and returns ScheduleGThreadLinked.SCHEDULE_LINK_REJECT_RESPONSE when you try to remove a running GThread.
        • pause(): To stop ScheduleGThreadLinked process without remove the remains gthreads which ready to run. And you can resume by call start() again.
        • stop(): To stop ScheduleGThreadLinked process and remove the remains gthreads which ready to run.
        • state():Returns the current state of ScheduleGThreadLinked
          • ScheduleGThreadLinked.SCHEDULE_LINK_TASKS_RUNNING: ScheduleGThreadLinked in running mode.
          • ScheduleGThreadLinked.SCHEDULE_LINK_TASKS_IDLE: ScheduleGThreadLinked is created but isn't started yet.
          • ScheduleGThreadLinked.SCHEDULE_LINK_TASKS_FINISHED: ScheduleGThreadLinked finished all gthreads which have.
          • ScheduleGThreadLinked.SCHEDULE_LINK_TASKS_PAUSED: ScheduleGThreadLinked is paused and it has gthreads need to run.

ScheduleGThread Exception

  • This exception happen when you try to add a running or terminated GThread to any container (SheduleGThread - SheduleGThreadLinked) at any point.

Library Link

Contact

You can connect to me for any bug, feedback or suggestion by this email: [email protected]