Thread: Multi-Threaded Design.

Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1 Multi-Threaded Design. 
    Extreme Donator


    Join Date
    Jul 2009
    Age
    27
    Posts
    4,351
    Thanks given
    826
    Thanks received
    1,239
    Rep Power
    1781
    Well, I was bored and was trying out some techniques and just messing around. Well, I decided that my application was going to be multi-threaded, what do you think of this kind of multi-threaded idea. This can be used for several things.
    • Context data startup
    • Player loading/saving
    • Networking (Not needed for netty)


    If you have any ideas on how I can improve, let me know!

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    
    <properties>
    	<entry key="server_thread">com/thomaslegodais/aura/thread/impl/ContextServerThread.java</entry>
    </properties>
    Code:
    /*
        An free -- open-source -- RuneScape emulator.
        Copyright (C) 2012, Thomas Le Godais <[email protected]>
    
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU Affero General Public License as
        published by the Free Software Foundation, either version 3 of the
        License, or (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Affero General Public License for more details.
    
        You should have received a copy of the GNU Affero General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
    package com.thomaslegodais.aura;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.util.Properties;
    import java.util.concurrent.ConcurrentLinkedQueue;
    import java.util.concurrent.Executor;
    import java.util.concurrent.Executors;
    
    import org.jboss.netty.bootstrap.ServerBootstrap;
    import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
    
    import com.thomaslegodais.aura.net.GameChannelPipelineFactory;
    import com.thomaslegodais.aura.thread.AbstractServerThread;
    import com.thomaslegodais.aura.util.ConsoleLogger;
    
    /**
     * The main application entry point. This class is used to load all of the
     * {@link AbstractServerThread}'s from the {@link Properties} file. So in lames:
     * gets everything up n' and running!
     * 
     * <p>
     * The main purpose of this class will be to perform 2 main priority functions.
     * The first will be to load all of the {@link AbstractServerThread}s. And, the
     * second one will to be successfully bind the server to a new
     * {@link InetSocketAddress}. We do not run the networking on its own thread,
     * since <b>Netty.io</b> networking already does that itself.
     * </p>
     * 
     * @author Thomas Le Godais <[email protected]>
     */
    public class Main {
    
    	/**
    	 * The {@link Executor} used for performing networking actions.
    	 */
    	private static final Executor exc = Executors.newCachedThreadPool();
    	
    	/**
    	 * The main entry point of the application, this is where everything that
    	 * was mentioned gets up and running!
    	 * 
    	 * @param args The command line arguments.
    	 */
    	@SuppressWarnings("unchecked")
    	public static void main(String... args) {
    		try {
    			
    			System.setOut(new ConsoleLogger(System.out));
    			System.setErr(new ConsoleLogger(System.err));
    			System.out.println("Bootstrapping Aura emulator...");
    			
    			Properties properties = new Properties();
    			properties.loadFromXML(new FileInputStream("./data/server-threads.xml"));
    			
    			String serverThreadName = properties.getProperty("server_thread");		
    			String fixedThreadName = serverThreadName = serverThreadName.replaceAll("/", ".").replace(".java", "");
    						
    			String[] threadRegex = null;
    			if(fixedThreadName.contains(","))
    				threadRegex = fixedThreadName.split(",");
    
    			ConcurrentLinkedQueue<Class<AbstractServerThread>> serverThreads = new ConcurrentLinkedQueue<Class<AbstractServerThread>>();
    			if(threadRegex != null)
    				for(int i = 0; i < threadRegex.length; i++)
    					serverThreads.offer((Class<AbstractServerThread>) Class.forName(threadRegex[i]));
    			else
    				serverThreads.offer((Class<AbstractServerThread>) Class.forName(fixedThreadName));
    			
    			for(Class<AbstractServerThread> threadClass : serverThreads) {
    
    				AbstractServerThread serverThread = threadClass.newInstance();
    				if(serverThread == null)
    					throw new RuntimeException("Specifed thread is NULL!");
    								
    				new Thread(serverThread).start();
    			}
    			System.out.println("Now running " + serverThreads.size() + " server threads, total active threads [active=" + Thread.activeCount() + "]");
    		} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
    			e.printStackTrace();
    		}
    		ServerBootstrap bootstrap = new ServerBootstrap();
    		bootstrap.setFactory(new NioServerSocketChannelFactory(exc, exc));
    		bootstrap.setPipelineFactory(new GameChannelPipelineFactory());
    		bootstrap.bind(new InetSocketAddress(43594));
    		System.out.println("Successfully bootstrapped Aura!");
    	}
    }
    Code:
    /*
        An free -- open-source -- RuneScape emulator.
        Copyright (C) 2012, Thomas Le Godais <[email protected]>
    
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU Affero General Public License as
        published by the Free Software Foundation, either version 3 of the
        License, or (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Affero General Public License for more details.
    
        You should have received a copy of the GNU Affero General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
    package com.thomaslegodais.aura.thread;
    
    /**
     * An {@link Thread} that is used to represent an individual thread in the server.
     * @author Thomas Le Godais <[email protected]>
     */
    public abstract class AbstractServerThread extends Thread implements Runnable {
    
    	/** 
    	 * The name of the thread we're creating.
    	 */
    	private String threadName;
    
    	/**
    	 * Constructs a new {@code AbstractServerThread} instance.
    	 * @param threadName The name of the thread.
    	 */
    	public AbstractServerThread(String threadName) {
    		this.threadName = threadName;
    		this.setName(getThreadName());
    	}
    
    	/**
    	 * Gets the name of the thread.
    	 * @return the thread name.
    	 */
    	public String getThreadName() {
    		return threadName;
    	}
    }
    Code:
    /*
        An free -- open-source -- RuneScape emulator.
        Copyright (C) 2012, Thomas Le Godais <[email protected]>
    
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU Affero General Public License as
        published by the Free Software Foundation, either version 3 of the
        License, or (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Affero General Public License for more details.
    
        You should have received a copy of the GNU Affero General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
    package com.thomaslegodais.aura.thread.impl;
    
    import com.thomaslegodais.aura.thread.AbstractServerThread;
    
    /**
     * An {@link AbstractServerThread} used for loading context for the server, by
     * this I am meaning anything not related the core/networking so things like
     * <b>ItemDefinition</b> will be loaded here.
     * 
     * @author Thomas <[email protected]>
     */
    public class ContextServerThread extends AbstractServerThread {
    
    	/**
    	 * Constructs a new {@code ContextServerThread} instance.
    	 */
    	public ContextServerThread() {
    		super("ContextServerThread");
    	}
    	
    	/*
    	 * (non-Javadoc)
    	 * @see java.lang.Thread#run()
    	 */
    	@Override
    	public void run() {
    	}
    }

    You can find my GitHub here, for what I'm currently working on.
    Reply With Quote  
     

  2. #2  
    Community Veteran

    WH:II:DOW's Avatar
    Join Date
    Dec 2007
    Age
    34
    Posts
    2,017
    Thanks given
    145
    Thanks received
    872
    Rep Power
    4275
    Quote Originally Posted by Sir Tom View Post
    Code:
    /*
        An free -- open-source -- RuneScape emulator.
        Copyright (C) 2012, Thomas Le Godais <[email protected]>
    
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU Affero General Public License as
        published by the Free Software Foundation, either version 3 of the
        License, or (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Affero General Public License for more details.
    
        You should have received a copy of the GNU Affero General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
    package com.thomaslegodais.aura.thread;
    
    /**
     * An {@link Thread} that is used to represent an individual thread in the server.
     * @author Thomas Le Godais <[email protected]>
     */
    public abstract class AbstractServerThread extends Thread implements Runnable {
    
    	/** 
    	 * The name of the thread we're creating.
    	 */
    	private String threadName;
    
    	/**
    	 * Constructs a new {@code AbstractServerThread} instance.
    	 * @param threadName The name of the thread.
    	 */
    	public AbstractServerThread(String threadName) {
    		this.threadName = threadName;
    		this.setName(getThreadName());
    	}
    
    	/**
    	 * Gets the name of the thread.
    	 * @return the thread name.
    	 */
    	public String getThreadName() {
    		return threadName;
    	}
    }
    ??? Thread (Java 2 Platform SE 5.0)
    hella titties
    Reply With Quote  
     

  3. Thankful users:


  4. #3  
    Community Veteran

    WH:II:DOW's Avatar
    Join Date
    Dec 2007
    Age
    34
    Posts
    2,017
    Thanks given
    145
    Thanks received
    872
    Rep Power
    4275
    This doesn't make any sense.
    hella titties
    Reply With Quote  
     

  5. #4  
    Registered Member

    Join Date
    Feb 2010
    Posts
    3,253
    Thanks given
    1,145
    Thanks received
    909
    Rep Power
    2081
    how can I learn to make a multi-threaded design as good as yours; please teach me!
    Reply With Quote  
     

  6. #5  
    Community Veteran

    WH:II:DOW's Avatar
    Join Date
    Dec 2007
    Age
    34
    Posts
    2,017
    Thanks given
    145
    Thanks received
    872
    Rep Power
    4275
    This looks like it's made of Raditz.
    hella titties
    Reply With Quote  
     

  7. Thankful user:


  8. #6  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    so you're telling me that all this is just starting threads... very badly. threads already implement runnable, so classes deriving thread don't need to implement it again. not to mention threads already have naming capability, you're just wasting space making a class that changes nothing. also why do the threads need names? in reality you're never going to need to know their names. forcing classes to derive from your abstractserverthread doesn't allow you the freedom to have your own class via them just submitting a class that implements the runnable interface and running them as tasks via executorservice or in your own implementation of a thread.

    Code:
    ConcurrentLinkedQueue<Class<AbstractServerThread>> serverThreads
    if multiple threads aren't going to access this collection then don't use concurrent collections.
    in reality, a properly formatted xml file wouldn't have multiple thread "names" per element. you should write your own xml reader. java's properties wasn't meant for this kind of use.

    tl;dr: you should just remove the need for a collection and use your executor service. also remove all the useless classes and use runnable correctly
    .
    Reply With Quote  
     

  9. Thankful user:


  10. #7  
    fumant viriditas quotidiana

    saifix's Avatar
    Join Date
    Feb 2009
    Age
    30
    Posts
    1,237
    Thanks given
    275
    Thanks received
    957
    Rep Power
    3304
    Code:
    abstract class AbstractServerThread extends Thread implements Runnable
    what

    java.lang.Thread.setName(...)
    java.lang.Thread(Runnable)
    "Im so bluezd out of my box.
    Im so fkd i canr even sens makeas Smoke blunt 420hash e tizday" - A legendary guy (1993 - 2015)
    Quote Originally Posted by nmopal View Post
    I will be creating a grimy dubstep song using these lyrics and vocaloid please prepare your bodies
    Reply With Quote  
     

  11. #8  
    Registered Member
    Join Date
    Jan 2010
    Posts
    125
    Thanks given
    9
    Thanks received
    29
    Rep Power
    25
    Quote Originally Posted by saifix View Post
    Code:
    abstract class AbstractServerThread extends Thread implements Runnable
    what

    java.lang.Thread.setName(...)
    java.lang.Thread(Runnable)
    Not only that, Thread already extends Runnable and already has a Thread(String) constructor.
    Reply With Quote  
     

  12. #9  
    Renown Programmer and Respected Member
    Maxi's Avatar
    Join Date
    Jun 2008
    Posts
    3,197
    Thanks given
    281
    Thanks received
    1,095
    Rep Power
    1366
    Quote Originally Posted by Supah Fly View Post
    also why do the threads need names? in reality you're never going to need to know their names. [/B]
    Giving threads names can be very useful so this statement is wrong and I'll tell you why. One example where it's useful is when you want to profile your application, knowing which thread is which is a must. If your application has a deadlock occurring or some kind infinite loop that consumes all resources before crashing the application, you can find in which thread(s) this is happening and which code is related to these problems.

    OT: why are you storing your threads in a concurrent collection? There is no need to do so. Also you have no easy control over your threads, the whole idea should be that you create functionality to control your threads in a way desired. All you have now is just a collection of threads that could execute something, which is useless, I'd rather execute stuff with an executor service like others already mentioned. You have no way to divide, separate or join logic between threads.
    Reply With Quote  
     

  13. Thankful users:


  14. #10  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    Quote Originally Posted by Maxi View Post
    Giving threads names can be very useful so this statement is wrong and I'll tell you why. One example where it's useful is when you want to profile your application, knowing which thread is which is a must. If your application has a deadlock occurring or some kind infinite loop that consumes all resources before crashing the application, you can find in which thread(s) this is happening and which code is related to these problems.
    i didn't say there's no point, so i'm not wrong. i'm just saying that he will probably never need to know them. i need them so i set the names for my threads.

    OT: why are you storing your threads in a concurrent collection? There is no need to do so. Also you have no easy control over your threads, the whole idea should be that you create functionality to control your threads in a way desired. All you have now is just a collection of threads that could execute something, which is useless, I'd rather execute stuff with an executor service like others already mentioned. You have no way to divide, separate or join logic between threads.
    not to mention the only way to end your application via code is System.exit() because they aren't being run as daemon threads.
    .
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 30
    Last Post: 12-04-2010, 05:49 PM
  2. [C#] Multi-threaded event system
    By Synthesized Insanity in forum Snippets
    Replies: 8
    Last Post: 03-02-2010, 10:27 PM
  3. Another multi threaded tut
    By life is hopeless in forum Tutorials
    Replies: 5
    Last Post: 08-24-2007, 10:44 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •