I see that there are a lot of way to program a game for example: i use paint() and a timer… someone paintComponent… other use thread with various method like rum cycle ecc ecc…
Now I see that the programming is different between methods , I have avoided to study thread because I wanted to study thread at university … but now if when start with thread i must restudy averythings then I start now …
What do you recommend me??
… what?
…mmmm …rum …I have a rum cycle every Friday night.
I think your question is should you start studying threads??? Yes, I suggest you do (and likely lots of others would suggest the same). They are mightily useful things, although they can cause you endless grief too if misused. So if you have the time, might as well start wrapping your head around them now.
And I don’t think that you’d necessarily have to “restudy everything”; you may find areas where you could use them in existing code, or just stick with a single-threaded model. It’s probably worth becoming familiar with the problems they can cause before you start using them everywhere. You should also learn when to use them, and when not to use them; they aren’t always needed and can just end up making things overly complicated.
Bad english = Bad problem…
I have study in a tutorial on zetcode …http://zetcode.com/tutorials/javagamestutorial/
I started with my first game using Timer class … Now I saw that all said that is better using Thread instead of Timer …
I saw also that the programming with Timer is completly different respect the programming with Thread… So given that i prefer attend to study Thread at university, I could continue to program with Timer or when i restart with Thread i must restudy all beacause the programming models is completly different??
Haha, how long are you programming already?
A thread is just something like a timer… Why learn it at university? LOL, just use it, you don’t have to ‘learn’ threads but USE them…
A thread is a loop so you can do 60 times per second (60 fps) something…
I would recommend you to watch some tutorials…
Here are some nice ones for you:
Do not mess with threads (yet).
Look up about game loops on this site or even better, use a framework like libgdx where you just can add on your render and logic stuff to a given predefined and working game architecture.
Actually, there is a lot to learn about threaded programming…
[quote]Haha, how long are you programming already? Wink
[/quote]
One year …
[quote]A thread is just something like a timer… Why learn it at university? LOL, just use it, you don’t have to ‘learn’ threads but USE them…
[/quote]
I hate this mentality , I program with Timer because i know what I am doing , use thread without know their working not is for me … Informatics is a logical form science not a form of handicraft…
[quote]Do not mess with threads (yet).
Look up about game loops on this site or even better, use a framework like libgdx where you just can add on your render and logic stuff to a given predefined and working game architecture.
[/quote]
Ok generally in this forum see game threads programmed , so your answer is to study threads…??
If you use it, you can experience it, just make a thread with a System.out.println(“hi thread”); And you will see what it does, and if you don’t understand, you google it, thats what I mean with using… Experience it yourself and look what it does…
And I said it already, its just like a timer, its a loop that gets runned continuously… But, when the thread runs it also saves some time for other processes, like the radio you listen to while playing the game…
Yes, and when you baked your first cookie, u knew everything about baking too, right…
Definitely not the correct attitude for learning about threads. Multithreading is something, that, when used correctly, improves the efficiency of your code. However, just throwing some code in a thread without knowing why or what the point is has no use. There is no point in just randomly putting code in a thread.
But if he sees a thousand text messages, he will understand its a loop, if you watch then that tutorial, where ulixava explains what it (exactly) does and learns how to use it (render/tick/sleep) you get an understanding of what it must be (I think, my opinion).
It helped for me, anyway…
Beppe,
From what I can gather, you are specifically talking about the use of threads in the context of game loops etc.? In that case I suggest that you have a look at this tutorial on game loops for starters:
If you would like to start learning about threading in general, then the Java tutorials can be a good place to start:
http://docs.oracle.com/javase/tutorial/essential/concurrency/
Yes, it may be very different to the way you are currently doing things, but there is always more than one way to skin a cat. It doesn’t hurt to broaden your horizons.
As for whether you should start learning threading now, or wait to learn it at university… Well that is entirely up to you. I guess you would cover it in more depth at university, but if you started teaching yourself the basics now, it may not feel so foreign when you finally get to the subject, and may aid you in your studies. But you really need to make that choice yourself.
Cheers,
nerb.
You are getting threads and loops mixed up.
Looping is like a person putting paper from one box into another repeatedly.
Multithreading is like multiple people taking paper from one box and putting it into another.
Threading fails when you have a chain of people passing down paper from one box the next and then someone slows down. You end up trying to take paper from an enpty box (null pointer). Of course this is just one way multithreading can go wrong.
@Herjan (& HeroesGraveDev)
Yes, Threads aren’t necessarily loops!
I’ll try to post some demo code:
public class LearnBasicThreading {
public static class TalkingRunnable implements Runnable {
private final String name;
public TalkingRunnable(String name) {
this.name = name;
}
public void run() {
sleep(100);
System.out.println("Thread " + name + "prints: Hello,");
sleep(100);
System.out.println("Thread " + name + "prints: I'm");
sleep(100);
System.out.println("Thread " + name + "prints: " + name);
}
}
public static void main(String[] args) {
System.out.println("I'm going to create a couple of threads.");
System.out.println("Let's see what they're saying!");
Runnable jack = new TalkingRunnable("Jack");
Runnable ben = new TalkingRunnable("Ben");
Runnable stanley = new TalkingRunnable("Stanley");
Thread jacksTask = new Thread(jack);
Thread bensTask = new Thread(ben);
Thread stanleysTask = new Thread(stanley);
jacksTask.start();
bensTask.start();
stanleysTask.start();
}
public static void sleep(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {}
}
}
My output:
I'm going to create a couple of threads.
Let's see what they're saying!
Thread Stanley prints: Hello,
Thread Ben prints: Hello,
Thread Jack prints: Hello,
Thread Stanley prints: I'm
Thread Ben prints: I'm
Thread Jack prints: I'm
Thread Stanley prints: Stanley
Thread Ben prints: Ben
Thread Jack prints: Jack
You can see how they all talk at the ‘same time’. Each one talks while the others talk as well.
Multithreading only means doing another thing at the same time:
import java.math.BigInteger;
public class LearnBasicThreading {
public static class CalculatingRunnable implements Runnable {
public void run() {
BigInteger i = new BigInteger("5");
i = i.pow(10000);
System.out.println("Calculated Stuff: " + i.toString());
}
}
public static void main(String[] args) {
System.out.println("This time I'll create a thread,");
System.out.println("which is going to calculate 5^10000.");
System.out.println("This is going to take a while, therefore");
System.out.println("I create another thread.");
Runnable calculation = new CalculatingRunnable();
Thread calculationTask = new Thread(calculation);
System.out.println("Starting thread...");
calculationTask.start();
System.out.println("Thread is running... still waiting for the answer...");
System.out.println("Is he finished yet?");
}
}
Output:
This time I'll create a thread,
which is going to calculate 5^10000.
This is going to take a while, therefore
I create another thread.
Starting thread...
Thread is running... still waiting for the answer...
Is he finished yet?
Calculated Stuff: 501237274920645200929755593374297774932156778133842583942142904227923953095078404018911069624842241336152184928620583720813274431768912730164821689547957811858742091111606122893848778402106681337735526081024957895721629712394089914022728984790421934879061713094772548592209108202603733257290166745030824473942446932040528665942308995221516922485809786795148731596082421380780454150838788867016523039922373131038641998175346338422877206054827564503235371169342436537325371943262018889434939990352804741697175505229254495331805689764874397989923275238841523573610576685490726802859819243653745732469692095356139819452233638457056770021400240594885718525026966278533101472229720144032290786176830303145623665755496360435599490323650572513025568234813704903083110444967390315478507643109709272470218811875684828428764846892265382195023886652123717092319225795594504960409044935368159521323778477017741777612142181821006771410662559184976660999925725059040493322464748516167697539730940448840082455864794427615836054554791385680376240555705705310640418914818432725632573900544375824289225110055381037572119488782802137254540743080359621754381456908672408840312897273852983806583141222160707728822053038238791525934900318958546031249932024098120872828088522712585702762195822969999737149010278411273267955845307034422962301693341819997141990129612648433981620894792079019269779218590761005440910777627130764809098131096632482838769231241354777310560506433916680450440171003075072638112146980748862972946289929742675258203358925444444756128485856884144877414969242528666963578568637990776356798944244368071275509913087832493228358727719463576937914165532431608736295823935850859602956501109544230207150342931374544601730528542990287355117226857435878992634677538783076731943101431231045219781733374608602456168971026916693136158979776343356397548327322689834826808251734636541021077910049779023642268457966054797804824736355644137564830595195451710237567879066439637258863004431176018386776125128648907629975770682763734191612197885648987874414165677042966711549856588653598408785500510685422317100298636543095941781200665686910806676041791991660514805632001035661874841652588320725707204618392142041484225612192457193974542099053730847597863337709199087948240520374012043494856466305707081247481972202946367819195209595885576564985479790372415190677667000598731151849814821780123592208917642142030152030072345481433382028329653192843312897129746236431981037750673145698908171385842694922445274810025912238416221522964351310630716198843932889821072030767508727091189405842713600224696110573235906544878846905387077857714046817137263818949931999780818432216728143326751002084575991984448963126429329855626472693024632620284389189569670626615141486480804667453766420052496208897899505339796907075749537925047639481099763394122954271668354214390461767075418949890757259815191735264809970480070623361261573386514423542116869959176227001051563415790066616730825389882550260549507356114926580661910358222186375052336626266396402920632455598407896677249075761328197632867250849378782054312978742146244372951153580324736875773015930589182005552613663380708208872090140751121108070186419534331882423382367231944116724985872384527946229533975200406009001175802553214211153583756335157100765941906016450622721056054785607907185614408709570609828729532194324073488303302169036381960864388576191326222063244533733156581375408659001285321684805335368617568283346867216329052732662620063658741831668787969703213340473542743542428508141091255419451769988466856089208450520741907702099600483335498521201163906681930066993336236094152793640529094686014991047088567105036260241152984467148870713490171710544611678764028106865825472006041963865952640276359450204310988738219705820340812476815469097461226193788465670121137455267380417275432557572871608813126741106427067715077700446202963319028927281969031661662735406952175070643446625878004294217525925732787544471487517642287816665575212425898205804148211536011850940665603144025098461900787902851141978648684835989682954921587387359499174492624708879549495025699365621180548950685097839585688157497849803802821714251372580052696412724434703674022001063816114251886079232743396991596837423584366398189226464881419833119900753928453879213723358991885164427168096353272861050545472688805451103643848013764843560502057319564217463145659707549025084226202528260927064071986667392548726459671172876646857330843589064031642100599431845417950711812059033443567112212757966231091347086555913182834014996628737859860706363179544558644455988199239032683220585664732242414913300291805168883958416214643036502478617783596157493232794260645582238010867705342559140264228808665610103214830997010446445153510172728006955669405306868569284423345052162076133101082758484204592864812776997646686279848506911866956485835893139608450871727100347954363706373163539008250530086790356183908280480933561398765472391006350924115874795796908769162764935162685121217250587170981896177333661824521517740754624065791036389274734921852267489894296164575044519359650614378654414198029034981769790845165837795906046315032565519352696462766106466774105071205787834267206183069164777673655331271336759603320460173126429741103074617460051610779377015122759270981845734044825361795166155820300919677836620236783353179010718633301247076648569928200445002452215470840284904784133729178494401821202806372418839442985275000609749798001855744591950991476089426637337222360186304365734543808390772428120270178709737770267613656623033355067765430185322107356853491344895509147430824382392603123813423730008797670095034073316230771848727933146860746745865795444364424130996435352645486982951723440522148302297083635331266905503420769986068776339277919600600368562314114319743568285546959663177509148945038701976847256620199663340609479213006239431672152430633078299619196445931106142003725770997007643638057570970191655409277156373433106416281124602277596026388331031116019967860952401669705220529365900799553267986640809567406320362311743977122119131956453604985607765737547440970987459542154102203452263287960968377547987715890093752997493407388257508916131741723862220177236888963407540326619079083531045575460224805677770108749031517837167683210260625791674114454386967291103177135211139321709106870148546832706900213216476908287228957454762017329216819341562992732298419110445451173007065816069528688872728858045204300785998475316449398597840062316796782985809958338705685529092798256957500833068994134050768770009905366006611480101959982790150501190128112172125707025128669728944606022132595881423926619388931735753935389312422762490037327411044350459969394535298110306704469261006337618669413404428685440302666300633585405872847618283921044509855877458728129059338704479794048883771725188957128263876122473094366610892593268417179320764593045997353942365453235709236555445002641997162224383683243256227625731486390554649930202182755869676383936027544370832840538165786625768305384553968906402587890625
Before you mess with threads, you better at least have a basic understanding of the Java Memory Model. In practice, you should stick to the classes in java.util.concurrent, which were written by people who actually understand the problems of concurrency.
I know basically like work Java I ’ ve study on the book of Tannembauw at university , and thread i think that are more near with MIC-3 and pipeline system…
However i decided to try with tread to read something tutorial in italian my language … In english i became crazy ;D
:point: This
Creating new threads for every little concurrent task is very inefficient, and I’d say it makes sense to generally treat threads as concurrent loops.
java.util.concurrent has great and easy to use tools for starting little tasks in multiple threads, without having to create new threads for each of them.
(but perhaps that was a bit besides the point of matheus23’s example)